Created
October 23, 2013 04:03
-
-
Save booxood/7112449 to your computer and use it in GitHub Desktop.
js inherist call apply prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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