Last active
August 29, 2015 14:03
-
-
Save hjzheng/bf0f4554dc600b903571 to your computer and use it in GitHub Desktop.
JavaScript继承(copy继承)
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继承 | |
//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