Created
August 14, 2014 05:35
-
-
Save zdying/7d6a4a893aa44777d17e to your computer and use it in GitHub Desktop.
javascript inheritance
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
function Person(name){ | |
this.name = name; | |
} | |
Person.prototype.sayName = function(){ | |
console.log(this.name); | |
} | |
function extend(subClass, superClass){ | |
function F(){} | |
F.prototype = superClass.prototype; | |
subClass.prototype = new F(); | |
subClass.prototype.constructor = subClass; | |
} | |
function Student(name){ | |
this.name = name | |
} | |
extend(Student, Person); | |
Student.prototype.setName = function(name){ | |
this.name = name; | |
} | |
var stu = new Student('Tom'); | |
stu.sayName(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment