`新函数()`用小写字母“f”在JavaScript中 [英] `new function()` with lower case "f" in JavaScript

查看:125
本文介绍了`新函数()`用小写字母“f”在JavaScript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的同事一直使用小写字母f的new function()来定义JavaScript中的新对象。它似乎在所有主流浏览器中都能正常工作,并且在隐藏私有变量方面似乎也相当有效。下面是一个例子:

  var someObj = new function(){
var inner ='some value';
this.foo ='blah';

this.get_inner = function(){
return inner;
};

this.set_inner = function(s){
inner = s;
};
};

只要使用this,它就成为someObj的公共属性。所以someObj.foo,someObj.get_inner()和someObj.set_inner()都可以公开使用。另外,set_inner()和get_inner()是特权方法,因此它们可以通过闭包访问内部。



然而,我没有看到任何引用这项技术在任甚至道格拉斯克罗克福德的JSLint也抱怨道:



  • 奇怪的结构。删除'new'


我们在生产中使用这种技术,它似乎运作良好,但我有点担心它,因为它没有记录在任何地方。有人知道这是否有效吗?

函数表达式,就像它是构造函数一样。



但是恕我直言,你可以用自动调用函数表达式实现,我真的没有看到使用 new 的要点>运算符:

  var someObj =(function(){
var instance = {},
inner ='some value';

instance.foo ='blah';

instance.get_inner = function(){
return inner;
$;

instance.set_inner = function(s){
inner = s;
};

返回实例;
})();

new 运算符的用途是创建新的对象实例,设置 [[Prototype]] 内部属性,您可以看到 [Construct] 内部属性。



以上代码会产生相同的结果。


My colleague has been using "new function()" with a lower case "f" to define new objects in JavaScript. It seems to work well in all major browsers and it also seems to be fairly effective at hiding private variables. Here's an example:

    var someObj = new function () {
        var inner = 'some value';
        this.foo = 'blah';

        this.get_inner = function () {
            return inner;
        };

        this.set_inner = function (s) {
            inner = s;
        };
    };

As soon as "this" is used, it becomes a public property of someObj. So someObj.foo, someObj.get_inner() and someObj.set_inner() are all available publicly. In addition, set_inner() and get_inner() are privileged methods, so they have access to "inner" through closures.

However, I haven't seen any reference to this technique anywhere. Even Douglas Crockford's JSLint complains about it:

  • weird construction. Delete 'new'

We're using this technique in production and it seems to be working well, but I'm a bit anxious about it because it's not documented anywhere. Does anyone know if this is a valid technique?

解决方案

I've seen that technique before, it's valid, you are using a function expression as if it were a Constructor Function.

But IMHO, you can achieve the same with an auto-invoking function expression, I don't really see the point of using the new operator in that way:

var someObj = (function () {
    var instance = {},
        inner = 'some value';

    instance.foo = 'blah';

    instance.get_inner = function () {
        return inner;
    };

    instance.set_inner = function (s) {
        inner = s;
    };

    return instance;
})();

The purpose of the new operator is to create new object instances, setting up the [[Prototype]] internal property, you can see how this is made by the [Construct] internal property.

The above code will produce an equivalent result.

这篇关于`新函数()`用小写字母“f”在JavaScript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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