从JS闭包创建对象:我应该使用“新”关键词? [英] creating objects from JS closure: should i use the "new" keyword?

查看:79
本文介绍了从JS闭包创建对象:我应该使用“新”关键词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我回答了一个问题关于这里SO封闭这个样本:

  function Constructor(){
var privateProperty ='private';
var privateMethod = function(){
alert('从公共方法调用');
};
return {
publicProperty:'im public',
publicMethod:function(){
alert('public from public method');
},
getter:privateMethod
}
}

var myObj = new Constructor();

// public
var pubProp = myObj.publicProperty;
myObj.publicMethod();
myObj.getter();

// private - 将导致错误
myObj.privateProperty
myObj.privateMethod

一个用户评论了我的回答说:


此外,如果你的函数显式返回一个对象,一个好的做法是使用来调用它,因为这样做是有误导性的 - 如果使用new,您会期望结果是构造函数的实例


我通常使用创建对象。但为什么不是一个好的习惯?它似乎使用新的和不使用新的返回相同的东西。从闭包创建对象的正确方法是什么?

解决方案

不,这不是一回事。考虑使用 instanceof

 函数C1(){
return {};
}

function C2(){
}

var c1 = new C1();
var c2 = new C2();
alert(c1 instanceof C1); // false;什么?
alert(c2 instanceof C2); // true,正如你所期望的。

这里是演示



因此,请通过分配 this 创建它们,忘记了 new

  function Constructor(){
var privateProperty ='private';
var privateMethod = function(){
alert('Called from private method');
};

this.publicProperty =我公开了!
this.publicMethod = function(){
alert('Called from public method');
};
this.getter = privateMethod;更好的是,尽可能使用原型:










$ b b

  function Constructor(){
var privateProperty ='private';
var privateMethod = function(){
alert('Called from private method');
};

this.getter = privateMethod;
}

Constructor.prototype.publicProperty =我公开!
Constructor.prototype.publicMethod = function(){
alert('Called from public method');
};


i answered one question about closures here in SO with this sample:

function Constructor() {
    var privateProperty = 'private';
    var privateMethod = function(){
        alert('called from public method');
    };
    return {
        publicProperty: 'im public',
        publicMethod: function(){
            alert('called from public method');
        },
        getter: privateMethod
    }
}

var myObj = new Constructor();

//public
var pubProp = myObj.publicProperty;
myObj.publicMethod();
myObj.getter();

//private - will cause errors
myObj.privateProperty
myObj.privateMethod

a user commented on my answer saying:

Also, if your function explicitly returns an object it is not a good practice to call it with new because that is misleading - if using new you'd expect the result to be an instance of Constructor

i usually create objects using new. but why is it not a good practice? it seems like using new and not using new returns the same thing. what is the proper way of creating objects from closures?

解决方案

No, it's not the same thing. Consider when using instanceof:

function C1() {
    return {};
}

function C2() {
}

var c1 = new C1();
var c2 = new C2();
alert(c1 instanceof C1); // false; wha...?
alert(c2 instanceof C2); // true, as you'd expect.

Here's a demo.

So instead, create them by assigning to this, possibly with a safeguard to prevent forgotten news.

function Constructor() {
    var privateProperty = 'private';
    var privateMethod = function() {
        alert('Called from private method');
    };

    this.publicProperty = "I'm public!";
    this.publicMethod = function() {
        alert('Called from public method');
    };
    this.getter = privateMethod;
}

Even better, use the prototype when possible:

function Constructor() {
    var privateProperty = 'private';
    var privateMethod = function() {
        alert('Called from private method');
    };

    this.getter = privateMethod;
}

Constructor.prototype.publicProperty = "I'm public!";
Constructor.prototype.publicMethod = function() {
    alert('Called from public method');
};

这篇关于从JS闭包创建对象:我应该使用“新”关键词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆