构造函数总是一个函数对象吗? [英] Is a constructor always a function object?

查看:100
本文介绍了构造函数总是一个函数对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读最新的ECMA-262参考资料,2011年6月第5.1版。

I'm reading the latest ECMA-262 reference, edition 5.1 June 2011.

8.6.2 表9我们对[[Construct]]内部属性有所了解:

In section 8.6.2 table 9 we have in regard to the [[Construct]] internal property:


创建一个对象。通过new运算符调用。 SpecOp的
参数是传递给
new运算符的参数。实现此内部方法
的对象称为构造函数。

Creates an object. Invoked via the new operator. The arguments to the SpecOp are the arguments passed to the new operator. Objects that implement this internal method are called constructors.

标准并未说明构造函数必须是功能对象。那么我们可以有一个不是函数对象的构造函数对象吗?

The standard doesn't say that a constructor has to be a Function object. So can we have a constructor object that is not a function object?

按要求链接到标准

推荐答案

定义了构造函数(如@RobG 指出),没有任何东西可以阻止非构造函数对象拥有 [[Construct]] 方法。

While the term "Constructor" is defined (as @RobG pointed out), there is nothing that prevents a non-"Constructor" object from having a [[Construct]] method.

这有点令人困惑。这意味着你可以在一个不是 Function 的对象上使用 new 运算符(因此不是构造函数每 4.3.4
),但确实提供 [[Construct]] method。

This is a bit confusing. It means you can use the new operator on an object that is not a Function (thus not a "constructor" as per 4.3.4 ), but does indeed provide a [[Construct]] method.

请注意,没有一个标准对象符合条件,但主机对象可能确实如此。像Java这样的浏览器插件可能会暴露一些像这样的对象:

Note that none of the standard objects qualify for that, but host objects may indeed. A browser plugin such as Java may expose some object like so:

new java.lang.String(); // it works, so java.lang.String has a [[Construct]] method
java.lang.String instanceof Function // false
Object.prototype.toString.call(java.lang.String).indexOf('Function') // -1

注意 typeof java.lang.String 返回function,即使 java.lang.String 是不是一个功能。根据 11.4.3 (这是一个<$ c的主机对象),这是正确的$ c> [[Call]] method)

Note that typeof java.lang.String returns "function" even though java.lang.String is not a function. This is correct according to 11.4.3 (it is a host object with a [[Call]] method)

这篇关于构造函数总是一个函数对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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