为什么有些 JavaScript 构造函数不是函数?

发布于 2024-11-28 22:36:13 字数 3756 浏览 1 评论 0原文

澄清:
“JavaScript 构造函数”应该更恰当地写为“javascript 构造函数”,以强调所考虑的构造函数不仅是原生 JavaScript 语言的构造函数,例如 Object、Array、Function 等,还包括其他 JavaScript 语言定义之外的构造函数但对于浏览器来说是固有的,例如 XMLHttpRequest,“JavaScript”一词旨在指示这些构造函数是使用 JavaScript 来表达和访问的。

一些参考:

换言之,存在对构造函数函数的引用,但没有对构造函数对象的引用!

(开玩笑,这是因为对象是函数,而函数也是对象!
为什么 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:

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.3

function 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静若繁花 2024-12-05 22:36:13

第一的:

对象是函数

不,不是:

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

toString 方法(这是在进行字符串连接时调用的方法)不是获取对象信息的可靠方法。如果我使用 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

所以你看,除了 Storage 之外,它们中的大多数实际上都是函数(为什么它这样做)不适用于存储,我不知道)。

另请记住,DOM 接口的行为可能与本机 JavaScript 对象不同。

另一方面,在 Chrome 中,toString 方法给出:

[object Function] 

[object Function] 

[object Function] 

function Storage() { [native code] } 

function XMLHttpRequest() { [native code] } 

function Worker() { [native code] } 

function FileReader() { [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. If I use typeof, I get the following:

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).

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:

[object Function] 

[object Function] 

[object Function] 

function Storage() { [native code] } 

function XMLHttpRequest() { [native code] } 

function Worker() { [native code] } 

function FileReader() { [native code] }
热鲨 2024-12-05 22:36:13

当您警告这些值时,浏览器引擎会警告 value.toString(),因此我们正在讨论为什么 Function.prototype.toString 的行为方式很奇怪。

ES5.1 规范指出:

15.3.4.2 Function.prototype.toString ( )
返回函数的依赖于实现的表示。该表示具有 FunctionDeclaration 的语法。

特别注意空白、行终止符和
表示字符串中的分号取决于实现。

toString 函数不是通用函数;如果 this 值不是 Function 对象,则会抛出 TypeError 异常。因此,它不能转移到其他类型的对象中作为方法使用。

显然,ES5 声明 toString 返回一个特定于实现的字符串。

如果您阅读 ES Harmony 提案 页面,其中指出:

函数到字符串 - 对有问题的 Function.prototype.toString (markm, allen) 的更详细规范

这里有更多资源:

基本上,函数对象(尤其是主机对象)上的 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 does Function.prototype.toString behave in a strange manner.

The ES5.1 specification states :

15.3.4.2 Function.prototype.toString ( )
An implementation-dependent representation of the function is returned. This representation has the syntax of a 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; it throws a TypeError exception if its this value is not a Function object. 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.

If you read the ES Harmony proposals page it states :

function to string – greater specification for problematic 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. 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.

婴鹅 2024-12-05 22:36:13

这个问题实际上可以解释为:

“JavaScript (ECMAScript) 语言约定是否适用于并限定浏览器的其他组件,例如与 DOM 接口的编程对象?”

最初的问题使用据称是 Function 类型并用作构造函数的对象。这些示例表明编程环境和 DOM 接口在表示方式上存在二分法。

如果存在这种实际的二分法,它是否明确?

这可能是实际问题。如果是这样,应将其放在原来的问题之前,以引导人们关注真正的问题。

参考资料:

ECMAScript 语言构造函数详细信息:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文