您的位置:首页 > 其它

原型继承例子,看各自的prototype和constructor

2016-07-28 22:04 267 查看
<script>
function superF(){
this.sup = true;
}

superF.prototype.getSuperValue = function(){
return this.sup;
}

function sub(){
this.subProperty = false;
}

sub.prototype = new superF();

var instance = new sub();

//    console.log(superF);
console.log(superF.prototype);                    //getSuperValue
console.log(sub.prototype);                      //sup,subProperty
console.log(instance.constructor);              //superF
console.log(sub.prototype.constructor);        //superF

</script>


三层继承:

<script>
function SuperType(){
this.color=["red","blue","green"];
this.name='Ant';
}

SuperType.prototype.showSuper = function(){
console.log(this.name);
}

function MidType(){
this.age = 10;
}

MidType.prototype.showMid = function(){
console.log(this.age);
}

function SubType(){
this.type='js';
}

MidType.prototype = new SuperType();

SubType.prototype = new MidType();

var instance_s1 = new SubType();
var instance_m1 = new MidType();

console.log("SuperType.prototype:------------------------------------------------");
console.log(SuperType.prototype);       //见执行结果图
console.log("MidType.prototype:----------------------------------------------------")
console.log(MidType.prototype);         //见执行结果图
console.log("SubType.prototype:-----------------------------------------------------");
console.log(SubType.prototype);         //见执行结果图

</script>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息