Skip to content

Instantly share code, notes, and snippets.

@hjzheng
Created July 5, 2014 13:46
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/93467949ab4f2993ec03 to your computer and use it in GitHub Desktop.
Save hjzheng/93467949ab4f2993ec03 to your computer and use it in GitHub Desktop.
JavaScript继承(类式继承)
//JavaScript 类式继承
//YUI2.9 YHAOO.lang.extend
function extend(Sub, Super) {
var F = function(){};
F.prototype = Super.prototype;
Sub.prototype = new F();
Sub.prototype.constructor = Sub;
}
function Person(name){
this.name = name;
}
Person.prototype.showName = function(){
console.log(this.name);
};
function Student(name, age){
Person.call(this, name);
this.age = age;
}
extend(Student, Person);
Student.prototype.showAge = function(){
console.log(this.age);
};
var s = new Student();
s.showAge();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment