javascript中继承(实现继承)的方式--简单原型链
1 .简单原型链事例
function Super() { };
Super.prototype = {
name:"Super",
sayName:function () {
alert(this.name);
}
}
function Sub() {
//Sub.prototype.name = "sub";
}
Sub.prototype = new Super();
var sub1 = new Sub();
sub1.sayName();//提示Super
2. 简单原型链和借用构造函数事例
function Super(name,age) {
this.name = name;
this.age = age;
this.color = ["red", "blue"];
};
Super.prototype = {
sayName:function () {
alert(this.name);
}
}
function Sub(name,age) {
Super.call(this,name,age);
}
Sub.prototype = new Super();
Sub.prototype.sayAge = function () {
alert(this.age);
}
var sub1 = new Sub("sub1",1);
sub1.sayName();//sub1
sub1.sayAge();//1
sub1.color.push("green");
alert(sub1.color); //red,blue,green
var sub2 = new Sub("sub2", 2);
sub2.sayName();//sub2
sub2.sayAge();//2
alert(sub2.color); //red,blue