为什么有些 JavaScript 构造函数不是函数?
澄清:
“JavaScript 构造函数”应该更恰当地写为“javascript 构造函数”,以强调所考虑的构造函数不仅是原生 JavaScript 语言的构造函数,例如 Object、Array、Function 等,还包括其他 JavaScript 语言定义之外的构造函数但对于浏览器来说是固有的,例如 XMLHttpRequest
,“JavaScript”一词旨在指示这些构造函数是使用 JavaScript 来表达和访问的。
一些参考:
- 在使用对象 - MDN 文档中使用构造函数
- “
构造函数
指定创建对象原型的函数“
在 对象 - MDN 文档 中的 - 示例:“...调用
Worker()
构造函数...”
Worker - MDN 文档 - 什么是预定义的原始构造函数?
- 诸如“new Image()”之类的构造函数在哪里和 `new Option()`,有记录吗?
- 所有 Javascript HTML 的文档在哪里元素构造函数?
换言之,存在对构造函数函数的引用,但没有对构造函数对象的引用!
(开玩笑,这是因为对象是函数,而函数也是对象!
为什么 JavaScript 中是函数既考虑构造函数又考虑对象?
更具体地说,对象,或者是 obj-eggs?,ARE,忽略文字实例,函数和函数的实例化是函数的对象实例。事实证明,函数是对象存在的基础,这是有争议的
7. 函数
优先于
8. 使用对象
在 MDN 文档中JavaScript 指南。第 8 节(我反对!)提供了使用构造函数和函数实例化创建对象所需的详细信息!)
为什么与 DOM 接口的构造函数不是函数?
javascript:
alert([
"using browser environment: \n"+window.navigator.userAgent,
Option, Image, Audio,
Storage, XMLHttpRequest, Worker, FileReader,
] . join("\n\n"));
向我们展示:
使用浏览器环境:
Mozilla/5.0(X11;U;Linux i686;en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3[对象选项]
[对象图像]
[对象音频]
[对象存储]
[对象 XMLHttpRequest]
[对象工作者]
[对象文件读取器]
但是 . ..
javascript:
alert([
XPCNativeWrapper,
].join("\n\n"));
(产生
函数 XPCNativeWrapper() { [本机代码] }
)
和 JavaScript 语言构造函数是函数。
javascript:
alert([
"using browser environment: \n"+window.navigator.userAgent,
Array, Boolean, Date, Function,
Number, Object, RegExp, String,
Error, Iterator,
].join("\n\n"));
给我们:
使用浏览器环境:
Mozilla/5.0(X11;U;Linux i686;en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3函数数组() { [本机代码] }
函数布尔() { [本机代码] }
函数日期() { [本机代码] }
函数函数() { [本机代码] }
函数编号() { [本机代码] }
函数对象() { [本机代码] }
函数正则表达式() { [本机代码] }
函数字符串() { [本机代码] }
函数错误() { [本机代码] }
函数迭代器() { [本机代码] }
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.
some references:
- Using a Constructor Function in Working with Objects - MDN Docs
- "
constructor
Specifies the function that creates an object's prototype"
in Object - MDN Docs - an example: "... calling the
Worker()
constructor ..."
Worker - MDN Docs - What are the predefined primitive constructors?
- Where are constructors such as, `new Image()` and `new Option()`, documented?
- Where is the Documentation for all of the Javascript HTML Element Constructors?
Rhetorically, there are references to constructor functions but NOT constructor objects!
(Facetiously, this is because Objects ARE functions, and Functions are objects!
Why in JavaScript is a function considered both a constructor and an object?
More specifically, objects, or is that obj-eggs?, ARE, ignoring literal instances, instantiations of functions and functions are Object instances of Functions. It is arguable that functions are fundamental to the existence of objects as evidenced by the fact
7. Functions
precedes
8. Working with Objects
in the MDN docs JavaScript Guide. That section 8, I object!, provides the details needed to create objects using constructors and function instantiations!)
Why are constructors that interface the DOM not functions?
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[object Option]
[object Image]
[object Audio]
[object Storage]
[object XMLHttpRequest]
[object Worker]
[object FileReader]
but ...
javascript:
alert([
XPCNativeWrapper,
].join("\n\n"));
(which produces
function XPCNativeWrapper() {
[native code] }
)
and JavaScript language constructors ARE functions.
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.3function Array() {
[native code] }function Boolean() {
[native code] }function Date() {
[native code] }function Function() {
[native code] }function Number() {
[native code] }function Object() {
[native code] }function RegExp() {
[native code] }function String() {
[native code] }function Error() {
[native code] }function Iterator() {
[native code] }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一的:
不,不是:
toString 方法(这是在进行字符串连接时调用的方法)不是获取对象信息的可靠方法。如果我使用
typeof
,我会得到以下结果:所以你看,除了
Storage
之外,它们中的大多数实际上都是函数(为什么它这样做)不适用于存储
,我不知道)。另请记住,DOM 接口的行为可能与本机 JavaScript 对象不同。
另一方面,在 Chrome 中,
toString
方法给出:First:
No, the are not:
The
toString
method (which is what gets called when you do string concatenation) is not a reliable way to get information about an object. If I usetypeof
, I get the following:So you see, most of them, apart form
Storage
, are actually functions (why it does not work forStorage
, I don't know).Also keep in mind that the DOM interface can behave differently than native JavaScript objects.
On the other hand, in Chrome the
toString
method gives this:当您警告这些值时,浏览器引擎会警告
value.toString()
,因此我们正在讨论为什么Function.prototype.toString
的行为方式很奇怪。ES5.1 规范指出:
显然,ES5 声明
toString
返回一个特定于实现的字符串。如果您阅读 ES Harmony 提案 页面,其中指出:
这里有更多资源:
Function.prototype.toString
的 Harmony 提案Function.prototype.toString
Function.prototype.toString
的邮件存档基本上,函数对象(尤其是主机对象)上的
toString
是一个已知问题这也是函数)是未定义的行为。 TC39 委员会已经致力于对此进行标准化。正如你所看到的,宿主对象被建议在 Strawman 中标准化,因此是否会进入 ES6 还没有定论。然而,生活在 ECMA 领域的函数对象应该在 ES6 中具有标准化的
toString
方法,如协调提案页面上所定义。When you alert those values the browser engine is alerting
value.toString()
so we are talking about why doesFunction.prototype.toString
behave in a strange manner.The ES5.1 specification states :
Clearly ES5 states that
toString
returns an implementation specific string.If you read the ES Harmony proposals page it states :
Here are some more resources :
Function.prototype.toString
Function.prototype.toString
Function.prototype.toString
Basically it's a known issue that
toString
on function objects (and especially host objects that are also functions) is undefined behaviour. The TC39 committee is already working on standardizing this.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. However function objects living in ECMA land should have a standardized
toString
method in ES6 as defined on the harmony proposals page.这个问题实际上可以解释为:
“JavaScript (ECMAScript) 语言约定是否适用于并限定浏览器的其他组件,例如与 DOM 接口的编程对象?”
最初的问题使用据称是 Function 类型并用作构造函数的对象。这些示例表明编程环境和 DOM 接口在表示方式上存在二分法。
如果存在这种实际的二分法,它是否明确?
这可能是实际问题。如果是这样,应将其放在原来的问题之前,以引导人们关注真正的问题。
参考资料:
ECMAScript 语言
构造函数
详细信息:引用 15 个标准内置 ECMAScript 对象 部分
引用部分 4 概述
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?"
The original question uses objects that are supposedly of type Function and used as constructors. The examples shows a dichotomy exists with the programming environment and the DOM interface in how they are represented.
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:quoting section 15 Standard Built-in ECMAScript Objects
quoting section 4 Overview