Last active
December 28, 2015 22:59
-
-
Save silianlinyi/7575583 to your computer and use it in GitHub Desktop.
推荐的原型继承方式
This file contains 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
/** | |
* Person类(父类) | |
*/ | |
function Person(name, age) { | |
this.name = name; | |
this.age = age; | |
} | |
Person.prototype.getName = function() { | |
return this.name; | |
} | |
Person.prototype.setName = function(name) { | |
this.name = name; | |
} | |
Person.prototype.getAge = function() { | |
return this.age; | |
} | |
Person.prototype.setAge = function(age) { | |
this.age = age; | |
} | |
function Student(name, age, major) { | |
Person.call(this, name, age); | |
this.major = major; | |
} | |
Student.prototype = new Person(); | |
Student.prototype.getMajor = function() { | |
return this.major; | |
} | |
Student.prototype.setMajor = function(major) { | |
this.major = major; | |
} | |
var s = new Student("zhangsan", 24, "计算机技术"); | |
s instanceof Person && s instanceof Student; // => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment