Created
July 5, 2014 13:46
-
-
Save hjzheng/93467949ab4f2993ec03 to your computer and use it in GitHub Desktop.
JavaScript继承(类式继承)
This file contains hidden or 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
//JavaScript 类式继承 | |
//YUI2.9 YHAOO.lang.extend | |
function extend(Sub, Super) { | |
var F = function(){}; | |
F.prototype = Super.prototype; | |
Sub.prototype = new F(); | |
Sub.prototype.constructor = Sub; | |
} | |
function Person(name){ | |
this.name = name; | |
} | |
Person.prototype.showName = function(){ | |
console.log(this.name); | |
}; | |
function Student(name, age){ | |
Person.call(this, name); | |
this.age = age; | |
} | |
extend(Student, Person); | |
Student.prototype.showAge = function(){ | |
console.log(this.age); | |
}; | |
var s = new Student(); | |
s.showAge(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment