函数真的是一个对象吗

发布于 2024-09-27 23:35:31 字数 894 浏览 3 评论 0原文

我是一名自学成才的 Web 开发人员,仍在努力掌握一些 JavaScript 基础知识。以下是摘自道格拉斯·克罗克福德 (Douglas Crockford) 的《好零件》(Good Parts) 的一些引述。

“JavaScript 中的函数是对象”

“在 JavaScript 中,数组是对象,函数是对象,正则表达式是对象,当然,对象也是对象”

“每个对象都链接到一个可以继承属性的原型对象”(即构造函数、toString 等)

如果 Function 是一个对象,那么为什么

 console.log(typeof Function);  // function 

是它的类型一个函数而不是对象

 console.log(Object.constructor);  // Function()

它是其“父级”的构造函数

 console.log(Function.constructor);  // Function()

困惑所以构造函数实际上是一个函数?

 console.log(typeof Function.prototype); // Function

它的原型类型是函数而不是对象吗? 我认为它继承自 Object

这些问题的答案将极大地帮助我对 JavaScript 的理解。

I am a self taught web developer and am still trying to come to grips with some JavaScript fundamentals. Below are some quotes extracted from Douglas Crockford's Good Parts.

"Functions in JavaScript are Objects"

"In JavaScript, arrays are objects, functions are objects, regular expressions are objects, and, of course, objects are objects"

"Every object is linked to a prototype object from which it can inherit properties" (namely constructor, toString, ...)

If Function is an Object then why

 console.log(typeof Function);  // function 

is its type a function and not object

 console.log(Object.constructor);  // Function()

is it the constructor of its 'parent'

 console.log(Function.constructor);  // Function()

puzzled so the constructor is in-fact a function?

 console.log(typeof Function.prototype); // Function

is the type of its prototype a function and not an object? i thought it inherited from Object

Answers to these questions will greatly assist my understanding on JavaScript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

关于从前 2024-10-04 23:35:31

如果函数是对象,那么为什么它的类型是函数而不是对象?

因为 typeof 运算符是这样定义的,可能是为了可用性:

  • Object (native and does notimplement [[Call]]) returns "object"
  • Object (native or host and doesimplement [[Call]]) returns "function"
  • Object (主机且未实现 [[Call]])
    返回实现定义的值,该值不能是“未定义”、“布尔值”、“数字”或“字符串”。

[[Call]] 是对象的内部属性,它将对象标识为函数(可调用)。非本机对象是由主机(例如浏览器)提供的对象,例如DOM对象或ActiveXObject的实例。

很困惑,构造函数实际上是一个函数?

为什么不会呢?构造函数是函数。实例只能使用函数来构造。 Object.constructor 是一个函数,但它也是一个对象。请参阅以下内容:

console.log((function () { }) instanceof Object);
//-> true

另外,来自 ECMAScript 规范:

每个内置函数和每个内置构造函数都有 Function 原型对象,它是表达式 Function.prototype (15.3.4) 的初始值,作为其 [[Prototype]] 内部属性的值。

除非另有说明,每个内置原型对象都具有 Object 原型对象,它是表达式 Object.prototype (15.2.4) 的初始值,作为其 [[Prototype]] 内部属性的值,除了对象原型对象本身。

另外,回答你最后的困惑:

Function 原型对象本身就是一个 Function 对象(其 [[Class]] 是“Function”),在调用时接受任何参数并返回未定义的值。

If Function is an Object then why is its type a function and not object?

Because the typeof operator is defined like that, probably for usability:

  • Object (native and does not implement [[Call]]) returns "object"
  • Object (native or host and does implement [[Call]]) returns "function"
  • Object (host and does not implement [[Call]])
    returns an Implementation-defined value that may not be "undefined", "boolean", "number", or "string".

[[Call]] is an internal property of an object that identifies the object as a function (callable). A non-native object is an object provided by the host (e.g. browser), such as a DOM object or an instance of ActiveXObject.

puzzled so the constructor is in-fact a function?

Why wouldn't it be? Constructors are functions. Instances can only be constructed using functions. Object.constructor is a function, but it's also an object. See the following:

console.log((function () { }) instanceof Object);
//-> true

Also, from the ECMAScript speficiation:

Every built-in function and every built-in constructor has the Function prototype object, which is the initial value of the expression Function.prototype (15.3.4), as the value of its [[Prototype]] internal property.

Unless otherwise specified every built-in prototype object has the Object prototype object, which is the initial value of the expression Object.prototype (15.2.4), as the value of its [[Prototype]] internal property, except the Object prototype object itself.

And also, to answer your final puzzlement:

The Function prototype object is itself a Function object (its [[Class]] is "Function") that, when invoked, accepts any arguments and returns undefined.

太阳男子 2024-10-04 23:35:31

当我们说“函数是一个对象”时,我们的意思并不是“是”,而是“而不是”,我们的意思与“猫是动物”的含义相同。如果有人问你养的是什么宠物,你不会回答“动物”。如果typeof总是响应object,那么它就毫无用处。

函数是一个对象,但是对于 typeof 返回来说这并不是一件有趣的事情,因为它是语言本身的静态质量,而不是需要在运行时报告的东西。

When we say, "a function is an object", we don't mean "is" as in "instead of", we mean it in the same sense as "a cat is an animal". If someone asked you what kind of pet you had, you wouldn't answer "an animal". typeof would be useless if it always responded object.

A function is an object, but that's not an interesting thing for typeof to return, since it's a static quality of the language itself, not something that needs to be reported at runtime.

波浪屿的海角声 2024-10-04 23:35:31

如果 typeof 运算符总是返回“object”,那么它就毫无用处,不是吗?一切都是对象,但也可以是其他东西。例如,字符串是一个对象,但它也是一个字符串:) 可以说,该运算符返回最具体类型的名称,而不是最通用的类​​型。这应该可以解释为什么 typeof Function 是“函数”。

至于constructor属性,构造函数是一个函数,在创建对象时由操作符new调用。它始终是一个函数,无论对象本身是ObjectFunction还是其他东西。

The typeof operator would be quite useless if it always returned "object", wouldn't it? Everything is an object, but it can be other things too. For example, a string is an object, but it is also a string :) The operator returns the name of the most specific type so to speak, not the most generic one. That should explain why typeof Functionis "function".

As for the constructor property, the constructor is a function that is invoked by the operator new when an object is created. It is always a function, regardless of whether the object itself is Object, Functionor something else.

物价感观 2024-10-04 23:35:31

console.log(typeof Function); 显示该对象的类型为 Function 而不是 object

给你一个例子:

function squareValue(var x) {
    return x * x;
}

可以松散地翻译为

var squareValue = new Function("x", "return x*x");

,因此,

var valueSquared = squareValue(x);

在这两个例子中的任何一个中执行,都会产生相同的结果......

这就是为什么 Javascript 中的每个函数都是一个 Function 对象。

javascript 对象/函数上的 .constructor.prototype.toString 都是各自对象中的函数,因此为什么输出为“功能”。

这是基于 ECMA-262 3rd版本 - 1999 年 12 月

希望这会有所帮助。

The console.log(typeof Function); shows that the object is of type Function and not object.

To give you an example:

function squareValue(var x) {
    return x * x;
}

can be loosely translated as

var squareValue = new Function("x", "return x*x");

and thus, doing

var valueSquared = squareValue(x);

in any of these 2 examples, will produce the same results...

Hence why every function in Javascript is a Function object.

The .constructor, .prototype, .toString on javascript Object/Function are all functions in their respective objects hence why you have the output as "function".

This is based according to the ECMA-262 3rd Edition -December 1999

Hope this helps.

╭ゆ眷念 2024-10-04 23:35:31

参考 每个 JavaScript 对象都是一个函数吗?

javascript:alert([ window.navigator.userAgent, Object, Function ].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

function Object() {
    [native code]
}

function Function() {
    [native code]
}

javascript:alert([ new Object, new Function ].join("\n\n") )

显示

[object Object]

function anonymous() {  }

javascript:alert([ new new Function ].join("\n\n") )

显示

[object Object]

reference Is every JavaScript Object a function?

javascript:alert([ window.navigator.userAgent, Object, Function ].join("\n\n") )

displays

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 Object() {
    [native code]
}

function Function() {
    [native code]
}

also

javascript:alert([ new Object, new Function ].join("\n\n") )

displays

[object Object]

function anonymous() {  }

and

javascript:alert([ new new Function ].join("\n\n") )

displays

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