js 新建方法

我们最常用的方法是:

//使用constructor模式

function a(user,pwd){ //use constructor 
this.user = user;
this.pwd = pwd;
return this;
}

//使用factory模式

function a2(user,pwd){ //use factory
var obj=new Object();
obj.user=user;
obj.pwd=pwd;
return obj;
}

//使用prototype模式

function O3(){ //use prototype 

O3.prototype.user='abc'; 
O3.prototype.pwd='dis'; 
// O3.propotype.get='get'; 
//O3.prototype.get(){ 
//alert(this.pwd); 
//} 

 

//使用constructor+prototype模式

function O4(user,pwd){ 
this.user=user; 
this.pwd=pwd; 
return this; 

O4.prototype.get=function(){console.log('123');} 

 

posted @ 2020-12-30 16:13  之鹿喵  阅读(188)  评论(0编辑  收藏  举报