工厂模型(计算矩形面积)
function rectangle(width,length){
return {
width:width,
length:length,
getArea:function(){
return (this.length * this.width)
}
}
}
button.onclick=function(){
let recArea=rectangle(txtwidth.value,txtlength.value);
alert(recArea.getArea());
}
构造函数(计算矩形面积)
function rectangle(width,length){
this.width=width;
this.length=length;
this.getArea=function(){
return (this.width * this.length);
}
}
button.onclick=function(){
let recArea=new rectangle (txtwidth.value,txtlength.value);
alert(recArea.getArea());
}
原型模型(计算矩形模型)
function rectangle(width,length){}
rectangle.prototype.width=0;
rectangle.prototype.length=0;
rectangle.prototype.getArea=function(){
return (this.width * this.length);
}
button.onclick=function(){
rectangle.prototype.width=txtwidth.value;
rectangle.prototype.length=txtlength.value;
let recArea=new rectangle();
alert(recArea.getArea());
}
混合模型(计算矩形面积)
function rectangle(width,length){
this.width=width;
this.length=length;
}
rectangle.prototype.getArea=function(){
return (this.width * this.length);
}
button.onclick=function(){
let recArea=new rectangle(txtwidth.value,txtlength.value);
alert(recArea.getArea())
}
工厂模式、构造函数、原型模型、混合模型
最新推荐文章于 2021-06-04 10:17:11 发布