Skip to content

Instantly share code, notes, and snippets.

@zdying
Created August 14, 2014 05:35
Show Gist options
  • Save zdying/7d6a4a893aa44777d17e to your computer and use it in GitHub Desktop.
Save zdying/7d6a4a893aa44777d17e to your computer and use it in GitHub Desktop.
javascript inheritance
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