使用new创建对象时,是否需要从构造函数返回RETURN

编程入门 行业动态 更新时间:2024-10-26 06:26:51
本文介绍了使用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:

  • 让ref成为评估NewExpression的结果。
  • 让构造函数为GetValue(ref)。
  • 如果Type(构造函数)不是Object,则抛出TypeError异常。
  • 如果构造函数未实现[[Construct]]内部方法,则抛出TypeError异常。
  • 返回在构造函数上调用[[Construct]]内部方法的结果,提供没有参数(即一个空的参数列表)。
  • Let ref be the result of evaluating NewExpression.
  • Let constructor be GetValue(ref).
  • If Type(constructor) is not Object, throw a TypeError exception.
  • If constructor does not implement the [[Construct]] internal method, throw a TypeError exception.
  • 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:

  • 让obj成为新创建的本机ECMAScript对象。
  • 按照8.12中的规定设置obj的所有内部方法。
  • 将obj的[[Class]]内部属性设置为Object。
  • 将obj的[[Extensible]]内部属性设置为true。
  • 让proto成为使用参数prototype调用F的[[Get]]内部属性的值。
  • 如果Type(proto)是Object ,将obj的[[Prototype]]内部属性设置为proto。
  • 如果Type(proto)不是Object,则设置[[Prototype] ] obj的内部属性,如标准的内置Object原型对象,如15.2.4所述。
  • 让结果成为调用内部[[Call]]的结果F的属性,提供obj作为此值,并将参数列表作为args提供给[[Construct]]。
  • 如果Type(result)是Object,则返回结果。
  • 返回obj。
  • Let obj be a newly created native ECMAScript object.
  • Set all the internal methods of obj as specified in 8.12.
  • Set the [[Class]] internal property of obj to "Object".
  • Set the [[Extensible]] internal property of obj to true.
  • Let proto be the value of calling the [[Get]] internal property of F with argument "prototype".
  • If Type(proto) is Object, set the [[Prototype]] internal property of obj to proto.
  • 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.
  • 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.
  • If Type(result) is Object then return result.
  • Return obj.
  • 更多推荐

    使用new创建对象时,是否需要从构造函数返回RETURN

    本文发布于:2023-11-05 19:21:57,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1561637.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:函数   对象   RETURN

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!