Skip to content

Instantly share code, notes, and snippets.

@booxood
Created October 23, 2013 04:03
Show Gist options
  • Save booxood/7112449 to your computer and use it in GitHub Desktop.
Save booxood/7112449 to your computer and use it in GitHub Desktop.
js inherist call apply prototype
var ClassA = function(name){
this.name = name || 'no name';
};
ClassA.prototype.sayName = function(){
console.log('say name:' + this.name);
};
var ClassB = function(name, age){
// this.new = ClassA;
// this.new(name);
// delete this.new;
// ClassA.call(this, name);
// ClassA.apply(this, [name]);
ClassA.apply(this, arguments);//继承属性
this.age = age;
};
ClassB.prototype = new ClassA();//继承方法
ClassB.prototype.sayAge= function(){
console.log('say age:' + this.age);
};
var a = new ClassA('A');
var b = new ClassB('B', 1);
a.sayName();//say name:A
b.sayName();//say name:B
b.sayAge(); //say age:1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment