简体   繁体   English

为什么一些JavaScript构造函数不起作用?

[英]Why are some JavaScript constructors not functions?

Clarification: 澄清:
"JavaScript constructor" should be more properly be written as "javascript constructor" to emphasize that the constructors considered are not just the native JavaScript language constructors, such as Object, Array, Function, etc. but also others, extrinsic to the JavaScript language definition but intrinsic to a browser, such as XMLHttpRequest , The word "JavaScript" is meant to indicate these constructors are expressed and accessed using JavaScript. 应该更恰当地将“JavaScript构造函数”编写为“javascript构造函数”,以强调所考虑的构造函数不仅仅是本机JavaScript语言构造函数,如Object,Array,Function等,还包括其他JavaScript语言定义的外在构造函数。但是浏览器固有的,例如XMLHttpRequest ,“JavaScript”一词意味着表示这些构造函数是使用JavaScript表达和访问的。

some references: 一些参考:

Rhetorically, there are references to constructor functions but NOT constructor objects ! 在修辞上,有对构造函数的引用,但不是构造函数对象

(Facetiously, this is because Objects ARE functions, and Functions are objects! (好笑,这是因为Objects ARE功能,而Function是对象!
Why in JavaScript is a function considered both a constructor and an object? 为什么JavaScript中的函数既是构造函数又是对象?
More specifically, objects, or is that obj-eggs?, ARE, ignoring literal instances, instantiations of functions and functions are Object instances of Functions. 更具体地说,对象,或者是obj-eggs?,ARE,忽略文字实例,函数和函数的实例化是函数的对象实例。 It is arguable that functions are fundamental to the existence of objects as evidenced by the fact 可以说,功能是物体存在的基础,事实证明了这一点
7. Functions 7. 功能
precedes 先于
8. Working with Objects 8. 使用对象
in the MDN docs JavaScript Guide . 在MDN文档JavaScript指南中 That section 8, I object!, provides the details needed to create objects using constructors and function instantiations!) 第8节,我反对!,提供了使用构造函数和函数实例创建对象所需的细节!)

Why are constructors that interface the DOM not functions? 为什么与DOM接口的构造函数不起作用?

javascript:
  alert([
    "using browser environment:  \n"+window.navigator.userAgent,
     Option, Image, Audio,
       Storage, XMLHttpRequest, Worker, FileReader,
   ] . join("\n\n"));

shows us: 告诉我们:

using browser environment: 使用浏览器环境:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3 Mozilla / 5.0(X11; U; Linux i686; en-US; rv:1.9.2.3)Gecko / 20100423 Ubuntu / 10.04(lucid)Firefox / 3.6.3

[object Option] [对象选项]

[object Image] [对象图片]

[object Audio] [对象音频]

[object Storage] [对象存储]

[object XMLHttpRequest] [object XMLHttpRequest]

[object Worker] [对象工作者]

[object FileReader] [object FileReader]

but ... 但......

javascript:
  alert([
             XPCNativeWrapper,
  ].join("\n\n"));

(which produces (产生

function XPCNativeWrapper() { [native code] } function XPCNativeWrapper(){[native code]}

)

and JavaScript language constructors ARE functions. 和JavaScript语言构造函数是ARE函数。

javascript:
  alert([
    "using browser environment:  \n"+window.navigator.userAgent,
             Array, Boolean, Date, Function,
               Number, Object, RegExp, String,
                 Error, Iterator,
  ].join("\n\n"));

gives us: 给我们:

using browser environment: 使用浏览器环境:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3 Mozilla / 5.0(X11; U; Linux i686; en-US; rv:1.9.2.3)Gecko / 20100423 Ubuntu / 10.04(lucid)Firefox / 3.6.3

function Array() { [native code] } function Array(){[native code]}

function Boolean() { [native code] } function Boolean(){[native code]}

function Date() { [native code] } function Date(){[native code]}

function Function() { [native code] } function Function(){[native code]}

function Number() { [native code] } 函数Number(){[native code]}

function Object() { [native code] } function Object(){[native code]}

function RegExp() { [native code] } 函数RegExp(){[native code]}

function String() { [native code] } function String(){[native code]}

function Error() { [native code] } 函数Error(){[native code]}

function Iterator() { [native code] } function Iterator(){[native code]}

First: 第一:

Objects ARE functions 对象是功能

No, the are not: 不,不是:

> a = function() {}
  function () {}
> a instanceof Object
  true
> b = {}
  Object
> b instanceof Function
  false

The toString method (which is what gets called when you do string concatenation) is not a reliable way to get information about an object. toString方法(在进行字符串连接时调用的方法)不是获取有关对象信息的可靠方法。 If I use typeof , I get the following: 如果我使用typeof ,我会得到以下内容:

using browser environment:  
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:5.0.1) Gecko/20100101 Firefox/5.0.1

function

function

function

object

function

function

function

So you see, most of them, apart form Storage , are actually functions (why it does not work for Storage , I don't know). 所以你看,他们中的大多数除了形成Storage ,实际上函数(它为什么不为工作Storage ,我不知道)。

Also keep in mind that the DOM interface can behave differently than native JavaScript objects. 还要记住,DOM接口的行为可能与本机JavaScript对象不同。

On the other hand, in Chrome the toString method gives this: 另一方面,在Chrome中toString方法给出了:

[object Function] 

[object Function] 

[object Function] 

function Storage() { [native code] } 

function XMLHttpRequest() { [native code] } 

function Worker() { [native code] } 

function FileReader() { [native code] }

When you alert those values the browser engine is alerting value.toString() so we are talking about why does Function.prototype.toString behave in a strange manner. 当您警告这些值时,浏览器引擎正在警告value.toString()因此我们讨论为什么Function.prototype.toString以奇怪的方式运行。

The ES5.1 specification states : ES5.1规范规定:

15.3.4.2 Function.prototype.toString ( ) An implementation-dependent representation of the function is returned. 15.3.4.2 Function.prototype.toString()返回函数的依赖于实现的表示形式。 This representation has the syntax of a FunctionDeclaration. 此表示具有FunctionDeclaration的语法。

Note in particular that the use and placement of white space, line terminators, and semicolons within the representation String is implementation-dependent. 请特别注意,表示字符串中的空格,行终止符和分号的使用和放置取决于实现。

The toString function is not generic; toString函数不是通用的; it throws a TypeError exception if its this value is not a Function object. 如果此值不是Function对象,则抛出TypeError异常。 Therefore, it cannot be transferred to other kinds of objects for use as a method. 因此,它不能转移到其他种类的对象用作方法。

Clearly ES5 states that toString returns an implementation specific string. 显然,ES5声明toString返回一个特定于实现的字符串。

If you read the ES Harmony proposals page it states : 如果您阅读ES Harmony提案页面,则说明:

function to string – greater specification for problematic Function.prototype.toString (markm, allen) 函数到字符串 - 有问题的Function.prototype.toString(markm,allen)的更大规范

Here are some more resources : 这是一些更多的资源:

Basically it's a known issue that toString on function objects (and especially host objects that are also functions) is undefined behaviour. 基本上,一个已知的问题是toString on function objects(尤其是也是函数的宿主对象)是未定义的行为。 The TC39 committee is already working on standardizing this. TC39委员会已经在努力使其标准化。

As you can see the host objects are proposed to be standardized in strawman so it's in the air whether that makes it into ES6. 正如你所看到的那样,主机对象被建议在草编中标准化,所以无论是否进入ES6,它都在空中。 However function objects living in ECMA land should have a standardized toString method in ES6 as defined on the harmony proposals page. 但是,生活在ECMA土地上的功能对象应该在和谐建议页面上定义的ES6中具有标准化的toString方法。

The question might actually be paraphrased as: 这个问题实际上可能被解释为:

"Do JavaScript (ECMAScript) language conventions apply to and qualify other components of the browser such as the programming objects that interface the DOM?" “JavaScript(ECMAScript)语言约定是否适用于浏览器的其他组件并对其进行限定,例如与DOM连接的编程对象?”

The original question uses objects that are supposedly of type Function and used as constructors. 原始问题使用假定为Function类型的对象并用作构造函数。 The examples shows a dichotomy exists with the programming environment and the DOM interface in how they are represented. 这些示例显示了编程环境和DOM接口在它们如何表示方面存在的二分法。

If there is this actual dichotomy, is it made explicit? 如果有这种实际的二分法,是否明确了?

This may be the actual issue. 这可能是实际问题。 If so, the original question should be preceded by this to direct attention to the real problem. 如果是这样,原始问题应该在此之前引导注意真正的问题。

references: 引用:

ECMAScript language constructor details: ECMAScript语言constructor详细信息:

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

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