Skip to content

Instantly share code, notes, and snippets.

@silianlinyi
Last active December 28, 2015 22:59
Show Gist options
  • Save silianlinyi/7575583 to your computer and use it in GitHub Desktop.
Save silianlinyi/7575583 to your computer and use it in GitHub Desktop.
推荐的原型继承方式
/**
* 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