什么版本的javascript引入任意构造函数返回? [英] What version of javascript introduced arbitrary constructor return?

查看:151
本文介绍了什么版本的javascript引入任意构造函数返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题中的某人有没有任何理由在构造函数中手动返回提到他认为在构造函数调用中返回任意对象可能已经在最新版本的Javascript中引入。



我现在无法访问备用浏览器,我找不到任何有关此信息,那么什么版本介绍了这个版本,或者它从来就存在了吗?


解决方案

如果你看看< a href =http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%201st%20edition,%20June%201997.pdf =nofollow> ECMA- 262 ed 1 (仍可从ECMA网站在线获取),您将看到:




生产 NewExpression:new NewExpression 的计算方式如下:


  1. 评估NewExpression。

  2. 调用GetValue(Result(1))。


  3. 如果Result(2)未实现内部[[Construct]]方法,则会生成运行时错误。调用Result(2)上的[[Construct]]方法,不提供任何参数(即空参数列表)。

  4. 如果Type(Result(5) ,会产生运行时错误。

  5. 返回结果(5)。



$ b b

然后在§15.3.2.1#18中解释 [[Construct]] 方法(下面,F是一个新构造的Function对象) / p>

>

18。将F的[[Construct]]属性设置为,当它被调用时,构造一个新对象,其[[Prototype]]属性等于
到[[Construct]]方法是
调用时的F.prototype的值(但是如果这个值不是一个对象,那么使用
的值Object.prototype),然后调用F作为一个函数(使用其
[[Call]]属性)与新对象作为此值和
赋给[[Construct]]方法的参数作为参数。如果调用[[Call]]方法的
结果是一个对象,那么该对象
将成为调用[[Construct]]方法的结果;
否则新对象将成为调用
[[Construct]]方法的结果。


因此,默认返回 this 的构造函数自ECMAScript开始就已经存在。


Someone in the question Is there any reason to manually return in a constructor function mentioned that he thought that returning arbitrary objects in constructor calls may have been introduced in a recent version of Javascript.

I am unable to access alternate browsers right now, and I can't find any information about this, so what version introduced this, or has it existed since forever?

If it hasn't been around for long, then what browser versions will support it?

解决方案

If you look in ECMA-262 ed 1 (which is still available online from the ECMA site) you will see:

11.2.2 The new operator

The production NewExpression : new NewExpression is evaluated as follows:

  1. Evaluate NewExpression.
  2. Call GetValue(Result(1)).
  3. If Type(Result(2)) is not Object, generate a runtime error.
  4. If Result(2) does not implement the internal [[Construct]] method, generate a runtime error.
  5. Call the [[Construct]] method on Result(2), providing no arguments (that is, an empty list of arguments).
  6. If Type(Result(5)) is not Object, generate a runtime error.
  7. Return Result(5).

And then in §15.3.2.1 #18 the [[Construct]] method is explained (in the following, F is a newly constructed Function object):

18. Set the [[Construct]] property of F to a method that, when it is invoked, constructs a new object whose [[Prototype]] property is equal to the value of F.prototype at the time the [[Construct]] method is invoked (but if this value is not an object then the value of Object.prototype is used), then invokes F as a function (using its [[Call]] property) with the new object as the this value and the arguments given to the [[Construct]] method as the arguments. If the result of invoking the [[Call]] method is an object, that object becomes the result of the invocation of the [[Construct]] method; otherwise the new object becomes the result of the invocation of the [[Construct]] method.

So a constructor returning this by default has been in ECMAScript since the beginning.

这篇关于什么版本的javascript引入任意构造函数返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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