js原型链案例代码与图解

function Person(name,age){
      this.name=name;
      this.age=age;
}
Person.prototype.hi=function(){
      console.log("Hi,my name is"+this.name+",I'm"+this.age+"years old now.");
};
Person.prototype.LEGS_NUM=2;
Person.prototype.ARMS_NUM=2;
Person.prototype.walk= function () {
      console.log(this.name+"is walking...");
};
 
function Student(name,age,className){
      Person.call(this,name,age);
      this.className=className;
}
Student.prototype=Object.create(Person.prototype);
Student.prototype.constructor=Student;
 
Student.prototype.hi=function(){
      console.log("Hi,my name is "+this.name+" ,I'm "+ this.age+" years old now,and from "+this.className+".");
};
 
Student.prototype.learn=function(subject){
     console.log(this.name+"is learning "+subject+" at "+this.className+".");
};
 
//test
var bosn=new Student("Bosn",27,"Class 3,Grade 2");
bosn.hi();               
bosn.LEGS_NUM;           
bosn.walk();             
bosn.learn("math");     

  

猜你喜欢

转载自www.cnblogs.com/dabai3330/p/12327798.html