牛客网_返回对象

这里写图片描述
我的答案:

function createModule(str1, str2) {
    /*var obj={
        greeting:str1,
        name:str2,
        sayIt :function(){
            return this.greeting+", "+this.name;
        }
    };
    return obj;*/

    //工厂模式
   /* var obj=new Object();
    obj={
       greeting:str1,
        name:str2,
        sayIt :function(){
            return this.greeting+", "+this.name;
        } 
    }
    return obj;*/

    //构造函数模式
    /*function CreateObj(){
        this.greeting=str1;
        this.name=str2;
        this.sayIt=function(){
               return  this.greeting+', '+this.name;
        }
    }

    return new CreateObj();*/

    //原型模式
    /*function CreateObj(){

    }
    CreateObj.prototype.greeting=str1;
    CreateObjt.prototype.name=str2;
    CreateObj.prototype.sayIt=function(){
        return this.greeting+', '+this.name;
    }
    return new CreateObj();*/

    //更简单的原型方法,字面量创建原型对象
    /*function CreateObj(){

    }
     CreateObj.prototype={
            greeting:str1,
            name:str2,
            sayIt:function(){
                return this.greeting+', '+this.name;
            }
        }
     Object.defineProperty(CreateObj.prototype,"constructor",{
         enumerable:false,
         value:CreateObj
     })
    return new CreateObj();*/

    //组合使用构造函数模式和原型模式
    /*function CreateObj(){
        this.greeting=str1;
        this.name=str2;
    }
    CreateObj.prototype.sayIt=function(){
        return this.greeting+', '+this.name;
    }
    return new CreateObj();*/

    //动态原型模式,,对上述方法进行封装
     /*function CreateObj(){
        this.greeting=str1;
        this.name=str2;

         if(typeof this.sayIt !=="function"){
              CreateObj.prototype.sayIt=function(){
                    return this.greeting+', '+this.name;
                }
         }
    }

    return new CreateObj();*/

    //寄生构造函数模式
    /*function CreateObj(){
        var o=new Object();
        o.greeting=str1;
        o.name=str2;
        o.sayIt=function(){
            return this.greeting+', '+this.name;
        }
        return o;
    }
    return new CreateObj();*/

    //稳妥构造函数模式
    function CreateObj(){
        var o=new Object();
        o.greeting=str1;
        o.name=str2;
        o.sayIt=function(){
            return o.greeting+', '+o.name;
        }
        return o;
    }
    return CreateObj();
}

更多讨论:
https://www.nowcoder.com/questionTerminal/48e53feaabe94506a61300edadb5496d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值