函数继承

1.继承

//父类
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 chile = new Sclass('Jack',18,'老司机');
chile.showName();

2.call,apply

二者都可以改变当前的this,区别在于apply方法要将参数放入数组中在传参

  function aa(a,b) {
    alert('我的this是' + this + ',我的a是' + a + ',我的b是' + b);
}
aa.call('abc',2,3);
aa.apply('abc',[2,3]);

你可能感兴趣的:(函数继承)