面向对象

2018-06-27  本文已影响0人  Khada

内置对象

面向对象

javascript对象
将相关的变量和函数组合成一个整体,这个整体叫做对象,对象中的变量叫做属性,变量中的函数叫做方法。javascript中的对象类似字典。

创建对象的方法

4、原型模式

<script type="text/javascript">
  function Person(name,age,job){        
        this.name = name;
        this.age = age;
        this.job = job;
  }
  Person.prototype.showname = function(){
        alert('我的名字叫'+this.name);    
  };
  Person.prototype.showage = function(){
        alert('我今年'+this.age+'岁');    
  };
  Person.prototype.showjob = function(){
        alert('我的工作是'+this.job);    
  };
  var tom = new Person('tom',18,'程序员');
  var jack = new Person('jack',19,'销售');
  alert(tom.showjob==jack.showjob);
</script>

5、继承

<script type="text/javascript">

function fclass(name,age){
    this.name = name;
    this.age = age;
}
fclass.prototype.showname = function(){
    alert(this.name);
}
fclass.prototype.showage = function(){
    alert(this.age);
}
function sclass(name,age,job)
{
    fclass.call(this,name,age);
    this.job = job;
}
sclass.prototype = new fclass();
sclass.prototype.showjob = function(){
    alert(this.job);
}
var tom = new sclass('tom',19,'全栈工程师');
tom.showname();
tom.showage();
tom.showjob();
</script>
上一篇 下一篇

猜你喜欢

热点阅读