Js prototype 实现继承

Person类:

function Person(name,age){
  this.name = name;
  this.age = age;
}

Prototype 继承:

Person.prototype.showName = function(){
  console.log("my Name is: " + this.name);
}

输出:

var people = new Person("chams",22);
people.showName(); //out: my Name is: chams

 

Student类:

function Student() {}

Prototype 继承:

Student.prototype = new Person("chamsStudent",22);
    var student = new Student();
    Student.prototype.height = "173";
    Student.prototype.showHeight = function(){
  console.log("my Height is: " + this.height);
}

输出:

student.showName();     //out: my Name is: chamsStudent
student.showHeight();    //out: my Height is: 173

 

 

posted @ 2013-12-30 14:40  jenkichams  阅读(943)  评论(0)    收藏  举报