一、前言:这个案例稍微有些难度,需要掌握基本的原型、原型链、构造函数、继承等相关知识,如若还没有掌握,不妨看看我的这篇博客,详细介绍了这些知识及关系:http://blog.youkuaiyun.com/spicyboiledfish/article/details/71123162
二、首先看看这个案例,源代码附上:
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;