JavaScript新功能混淆 [英] JavaScript new Function confusion

查看:115
本文介绍了JavaScript新功能混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读Learning JavaScript 2nd edition的过程中,我会在第13章创建自定义JavaScript对象中看到一段代码。

  var oneOff = new Function(){
this.variablea =variablea;
this.method = function(){
return xxx;


$ / code>

据说它和

  var oneOff = new Object(); 
oneOff.variablea =variablea;
oneOff.method = function(){...};

但我不知道新的Function(){..}是什么。任何帮助表示赞赏。感谢。

解决方案

使用新函数(){/ * * /}时,您并不是创建一个新函数,而是一个对象。
这仅仅是一个新的构造函数的标准用法,如下所示:

  var oneInstance = new ConstructorFunction(); 

函数ConstructorFunction(){
this.var1 ='val1';
this.method1 = function(){};

不同之处在于构造函数在实例化时被定义好了,我们省略了调用这个函数,这是合法的(尽管我觉得很困惑)。

我发现它用一些括号清楚地表明了对构造函数的调用:

  var oneOff = new(function(){
this.variablea =variablea;
this .method = function(){
return xxx;
};
})();

但是为了创建一个单独的对象,我们宁愿使用简单的对象表示法:

  var oneOff = {
variablea:'valuea',
method:function(){return xxx; }
}

他们在表演中的表现如何?



对于新的function(){}方法,创建对象需要先创建一个函数,然后在新建上下文中执行该函数。
另一方面,对象只是创建一个对象,所以创建时间更少,垃圾更少。


在使用两种类型的对象(读取/写入属性/使用方法的时间)的性能时,很难说所有JavaScript解释器都非常复杂,非常不一样。在基准测试之前,他们何时以及如何决定优化部分代码是您无法真正了解的。 - 基准测试也不是一件容易的事,因为这些随机优化。我敢打赌,他们应该使用性能非常接近,让我们知道如果你花时间做一些措施。

While reading Learning JavaScript 2nd edition, I see a block of code in Chapter 13: Creating Custom JavaScript Objects.

var oneOff = new Function () {
    this.variablea = "variablea";
    this.method = function () {
        return xxx;
    }
}

It is said that it is the same as:

var oneOff = new Object();
oneOff.variablea = "variablea";
oneOff.method = function () {...};

But I don't know what new Function () {..} is. Any help is appreciated. Thanks.

解决方案

When using new function() { /* */ }, you are not creating a new function, but rather an object.
It is just a standard use of new with a constructor function, like in :

var oneInstance = new ConstructorFunction();

function ConstructorFunction() {
         this.var1 = 'val1';
         this.method1 = function() { } ;
}

The difference is that the constructor is defined right at the time of instantiation, and we omit to call the function, which is legal (although confusing i think).

I find it somehow clearer with some brackets, and making explicit the call to the constructor :

var oneOff = new  ( function () {
  this.variablea = "variablea";
  this.method = function () {
    return xxx;
  };
} ) () ;

but for creating just one single object, one would rather use the simple object notation :

var oneOff = {
  variablea : 'valuea',
  method    : function () { return xxx; }
}

How do they compare in performances ?

For the new function() {} method, creating the object will require to first create a function, then execute that function within a 'new' context. On the other hand, object litteral just create ...one object, so it's less time and less garbage created.

On use performances of the two types of objects (time to read/write a property /use a method) it's very hard to say since all javascript interpreters are very complex and very different. When and how do they decide to optimize a part of the code is something you can't really know before benchmarking. - And benchmarking is not an easy task also because of those 'random' optimisations. My bet is that they should be very close in use performance, let us know if you take the time to do some measures.

这篇关于JavaScript新功能混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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