简体   繁体   English

在这种情况下,New是否有意义?

[英]Does New make any sense in this case?

Is there any case where a Constructor returns something on purpose and it still makes sense to new it? 在任何情况下,构造函数会故意返回某些东西,而对于new东西仍然有意义吗?

var Fn = function(){
    return this.fn = function(){}
}

var a = new Fn()
var b = Fn()

I can't seem to find any difference between a and b from console, but just in case I missed something, are they identical? 我似乎无法从控制台找到ab之间a任何区别,但是以防万一我错过了一些东西,它们是一样的吗? Apart from b 's side effect of adding fn as a method to window object. 除了bwindow对象添加fn作为方法的副作用之外。 And if they're the same, does it mean that when a Constructor returns something, it's no longer a Constructor and shouldn't be new ed? 如果它们相同,是否意味着当构造函数返回某些内容时,它不再是构造函数,并且不应该new编写吗?

Edit, the reason I'm asking is that I'm working with Coffee and Angular at the moment, while both seem to be pretty sensitive about return , especially with providers. 编辑,我要问的原因是我目前正在使用Coffee和Angular,而两者似乎都对return非常敏感,尤其是对于提供者。 Do you have any best practises regarding this? 您对此有最佳做法吗?

Yes. 是。 The new invocation creates a new this context in which the function inside the constructor will be evaluated. new调用将创建一个新的this上下文,在this上下文中将评估构造函数内部的函数。 Without it, the invocation assumes the local context and attaches your function to it. 没有它,调用将假定本地上下文并将您的函数附加到该本地上下文。 This is really important if you have multiple functions that you want to bind a unique object to. 如果您要将多个功能绑定到一个唯一的对象,那么这非常重要。

I had the same question you did, and read the spec. 我也有同样的问题,请阅读规范。 I wrote about this in What the 'new' operator means to Javascript . 在“ new”运算符对Javascript的意义中对此进行了介绍。

Arun's answer is half-right, in that it's true for the special case that you're working at the base level of the Javascript interpreter, where this === windows (or, for Node, this === global ; for PLv8, this === role ). Arun的回答是正确的,因为在特殊情况下,您确实是在Java语言解释器的基础级别上工作,在this === windows (对于Node, this === global ;对于PLv8, this === role )。 Inside other objects (a really common occurrence when working with modern libraries like jQuery or Backbone!), the this operator is the local context, not the windows root. 在其他对象内部(使用jQuery或Backbone等现代库时,这确实很常见!), this运算符是本地上下文, 而不是 Windows根目录。

So with the information @Elf provided, as in 因此,使用提供的@Elf信息,如

When the [[Construct]] property for a Function object F is called, the following steps are taken: 调用功能对象F的[[Construct]]属性时,将执行以下步骤:

  1. Create a new native ECMAScript object. 创建一个新的本机ECMAScript对象。
  2. Set the [[Class]] property of Result(1) to “Object”. 将Result(1)的[[Class]]属性设置为“ Object”。
  3. Get the value of the prototype property of the F. 获取F的prototype属性的值。
  4. If Result(3) is an object, set the [[Prototype]] property of Result(1) to Result(3). 如果Result(3)是对象,则将Result(1)的[[Prototype]]属性设置为Result(3)。
  5. If Result(3) is not an object, set the [[Prototype]] property of Result(1) to the original Object prototype object as described in 15.2.3.1. 如果Result(3)不是对象,则按15.2.3.1所述将Result(1)的[[Prototype]]属性设置为原始Object原型对象。
  6. Invoke the [[Call]] property of F, providing Result(1) as the this value and providing the argument list passed into [[Construct]] as the argument values. 调用F的[[Call]]属性,提供Result(1)作为this值,并提供传递给[[Construct]]的参数列表作为参数值。
  7. If Type(Result(6)) is Object then return Result(6). 如果Type(Result(6))是Object,则返回Result(6)。
  8. Return Result(1). 返回结果(1)。

When the Constructor intentionally returns something, that something would get returned in Result(6), therefore defying the purpose of Constructors--to return Result(1) as an instance. 当构造函数有意返回某些内容时,会在Result(6)中返回某些内容,因此违反了构造函数的目的-将Result(1)作为实例返回。

The only possible use of this is to take advantage of Step 6 and call the Fn with certain arguments and certain this , which, could easily be done without using Constructors and a simple call . 唯一可能的用途是利用第6步,并调用Fn某些参数和某些this ,其中,可以很容易地在不使用构造函数和一个简单的进行call So I guess, if Constructors return stuff, it ain't Constructor anymore. 所以我想,如果构造函数返回东西,它就不再是构造函数。

Edit: 编辑:

The interesting exception being, the object returned is Result(1) on purpose, in order to be able to take arbitrary number of arguments: Use of .apply() with 'new' operator. 有趣的例外是,故意返回的对象是Result(1),以便能够接受任意数量的参数: 将.apply()与'new'运算符一起使用。 Is this possible? 这可能吗? . Thanks @Elf. 谢谢@Elf。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM