//组合使用构造函数模式和原型模式
function Person(name,skill,country) {
this.name=name;
this.age=skill;
this.country=country;
this.member=["刘封","刘婵"];
} //机器可以听命令
Person.prototype={
constructor:Person,
sayName:function () {
alert(this.name);
}
} //还可以用模板
var person1=new Person('马超','铁骑','蜀国');
var person2=new Person('刘备','仁德','蜀国');
function Person(name,skill,country) {
this.name=name;
this.skill=skill;
this.country=country;
if(typeof this.sayCountry !=undefined){
Person.prototype.sayCountry=function () {
alert(this.country);
};
}
}
var friend=new Person('张飞','咆哮','蜀国');
friend.sayCountry();
//父函数,机器A,A类人。它的实例a中有[[Prototype]]属性和自定义的property属性
function SuperType(){
this.property=true;
}
//在SuperType原型对象(模具A)中添加getSuperValue方法
SuperType.prototype.getSuperValue=function(){
return this.property
}
//子函数,机器B,B类人。构造函数SubType,它的实例中有[[Prototype]]属性和自定义的subproperty属性
function SubType(){
this.subproperty=false;
}
//继承了SuperType (原型链)
SubType.prototype=new SuperType(); //机器B=a
//在SubType原型对象(模具B)中添加getSubValue方法
SubType.prototype.getSubValue=function(){
return tis.subproperty;
};
var insatance=new SubType();
alert(insatance.getSuperValue()); //true
function SuperType(){
this.colors=['red','blue','green'];
}
function SubType(){
//继承了SuperTYpe
SuperType.call(this);
}
var insatance1=new SubType();
insatance1.colors.push('black');
alert(insatance1.colors);// 'red,blue,green,black'
var insatance2=new SubType();
alert(insatance2.colors);//'red,blue,green'
function SuperType(name){
this.name=name;
}
function SubType(){
//继承了SuperTYpe,同时还传递了参数
SuperType.call(this,'赵云');
//实例属性
this.age=29;
}
var insatance=new SubType();
alert(insatance.name); //赵云
alert(insatance.age); //29
function SuperType(name){
this.name=name;
thi.colors=['red','blue','green'];
}
SuperType.prototype.sayName=function(){
alert(this.name);
};
function SubType(name,age){
//继承属性
SuperType.call(this,name);
this.age=age;
}
//继承方法
SubType.prototype=new SuperType();
SubType.prototype.sayAge=function(){
alert(this.age);
}
var instance1=new SubType('zhaoyun',29);
instance1.colors.push('black');
alert(instance1.colors); //'red,blue,green,black'
instance1.sayName();//zhaoyun
instance1.sayAge();//29
var insatance2=new SubType('诸葛瑾',25);
alert(instance2.colrs);'red,blue,green'
instance22.sayName();//诸葛瑾
instance2.sayAge();//25
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有