使用new创建对象时,是否需要从构造函数返回RETURN [英] Is RETURN from constructor necessary when creating object with new

查看:171
本文介绍了使用new创建对象时,是否需要从构造函数返回RETURN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的功能:

function Apple(){
    this.color = "green";
    return this;
}

创建这样的对象时:

var my_obj = new Apple();

该行返回此项; 必要且/或它是否通过语言参考有效?

is that line return this; necessary and/or is it valid by language reference?

推荐答案

不,返回没有必要,但它是有效的。如果返回的值是一个对象, new 将返回该对象而不是新创建的对象。

No, returning this is not necessary, but it is valid. If the returned value is an object, new will return that object instead of the newly created object.

查看点 ECMAScript 5 :

new运算符在构造函数(通常是函数)上调用内部[[Construct]]方法:

The new operator calls the internal [[Construct]] method on the constructor (usually a function):


11.2.2新运算符

11.2.2 The new Operator

生成NewExpression:new NewExpression的计算方法如下:

The production NewExpression : new NewExpression is evaluated as follows:


  1. 让ref成为评估NewExpression的结果。

  2. 让构造函数为GetValue(ref)。

  3. 如果Type(构造函数)不是Object,则抛出TypeError异常。

  4. 如果构造函数未实现[[Construct]]内部方法,则抛出TypeError异常。

  5. 返回在构造函数上调用[[Construct]]内部方法的结果,提供没有参数(即一个空的参数列表)。

  1. Let ref be the result of evaluating NewExpression.
  2. Let constructor be GetValue(ref).
  3. If Type(constructor) is not Object, throw a TypeError exception.
  4. If constructor does not implement the [[Construct]] internal method, throw a TypeError exception.
  5. Return the result of calling the [[Construct]] internal method on constructor, providing no arguments (that is, an empty list of arguments).


[ [构造]]函数的内部方法在第13.2.2节中描述:

The [[Construct]] internal method of functions is described in point 13.2.2:


13.2.2 [[Construct]]

13.2.2 [[Construct]]

当使用可能为空的参数列表调用Function对象F的[[Construct]]内部方法时,将采取以下步骤:

When the [[Construct]] internal method for a Function object F is called with a possibly empty list of arguments, the following steps are taken:


  1. 让obj成为新创建的本机ECMAScript对象。

  2. 按照8.12中的规定设置obj的所有内部方法。

  3. 将obj的[[Class]]内部属性设置为Object。

  4. 将obj的[[Extensible]]内部属性设置为true。

  5. 让proto成为使用参数prototype调用F的[[Get]]内部属性的值。

  6. 如果Type(proto)是Object ,将obj的[[Prototype]]内部属性设置为proto。

  7. 如果Type(proto)不是Object,则设置[[Prototype] ] obj的内部属性,如标准的内置Object原型
    对象,如15.2.4所述。

  8. 让结果成为调用内部[[Call]]的结果F的属性,提供obj作为此值,并将参数列表作为args提供给[[Construct]]。

  9. 如果Type(result)是Object,则返回结果。

  10. 返回obj。

  1. Let obj be a newly created native ECMAScript object.
  2. Set all the internal methods of obj as specified in 8.12.
  3. Set the [[Class]] internal property of obj to "Object".
  4. Set the [[Extensible]] internal property of obj to true.
  5. Let proto be the value of calling the [[Get]] internal property of F with argument "prototype".
  6. If Type(proto) is Object, set the [[Prototype]] internal property of obj to proto.
  7. If Type(proto) is not Object, set the [[Prototype]] internal property of obj to the standard built-in Object prototype object as described in 15.2.4.
  8. Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.
  9. If Type(result) is Object then return result.
  10. Return obj.


这篇关于使用new创建对象时,是否需要从构造函数返回RETURN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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