代码
<script>
//这个函数最终返回一个对象,这个对象拥有传入对象o的全部属性和方法。从而实现了继承
//object函数,传入一个对象
function object(o) {
//创建一个空构造函数
function F() { }
//这个空构造函数的原型对象指向o这个对象
F.prototype = o;
//返回F的实例对象
return new F();
}
//主要是这个函数,实现子类原型对象只继承父类原型对象中的属性
function inheritPrototype(subType, superType) {
var prototype = object(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
}
function Father(name) {
this.name = name;
this.color = ["red", "blue"];
}
Father.prototype.SayName = function () {
console.log(this.name);
}
function Son(name,age) {
Father.call(this, name);
this.age = age;
}
inheritPrototype(Son, Father);
Son.prototype.SayAge = function () {
console.log(this.age);
}
</script>
浙公网安备 33010602011771号