Skip to content

Instantly share code, notes, and snippets.

@hjzheng
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjzheng/bf0f4554dc600b903571 to your computer and use it in GitHub Desktop.
Save hjzheng/bf0f4554dc600b903571 to your computer and use it in GitHub Desktop.
JavaScript继承(copy继承)
//JavaScript继承
//copy继承(call and prototype)
function extend(obj1, obj2){
for(var attr in obj2){
obj1[attr] = obj2[attr];
}
}
function Person(name){
this.name = name;
}
Person.prototype.sayName = function(){
console.log(this.name);
};
function Student(name, age){
Person.call(this, name);
this.age = age;
}
Student.prototype.sayAge = function(){
console.log(this.age);
};
extend(Student.prototype, Person.prototype);
var s = new Student("hurry", 26);
s.sayName();
s.sayAge();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment