js继承有好多种实现形式,下面来讲一下基于类的继承
function person (name,age) {
this.name = name;
this.age = age;
this.like= [];
}
person.prototype.say = function ()
{
console.log(this.name + " , " + this.age +" , " + this.like);
}
function student (name, age, no) {
person.call(this,name,age);
this.no = no;
}
student .prototype = new person();
var stu1 = new student ("0001","张三",11);
stu1.like.push("java");
stu1.say();
var stu2 = new student ("0002","李四",12);
stu2.like.push("sql");
stu2.like.push("javaScript");
stu2.say();
输出结果
0001 , 张三 , java
VM220:8 0002 , 李四 , sql,javaScript