函数真的是一个对象吗
我是一名自学成才的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为 typeof 运算符是这样定义的,可能是为了可用性:
返回实现定义的值,该值不能是“未定义”、“布尔值”、“数字”或“字符串”。
[[Call]]
是对象的内部属性,它将对象标识为函数(可调用)。非本机对象是由主机(例如浏览器)提供的对象,例如DOM对象或ActiveXObject的实例。为什么不会呢?构造函数是函数。实例只能使用函数来构造。
Object.constructor
是一个函数,但它也是一个对象。请参阅以下内容:另外,来自 ECMAScript 规范:
另外,回答你最后的困惑:
Because the typeof operator is defined like that, probably for usability:
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.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:Also, from the ECMAScript speficiation:
And also, to answer your final puzzlement:
当我们说“函数是一个对象”时,我们的意思并不是“是”,而是“而不是”,我们的意思与“猫是动物”的含义相同。如果有人问你养的是什么宠物,你不会回答“动物”。如果
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 respondedobject
.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.如果
typeof
运算符总是返回“object”,那么它就毫无用处,不是吗?一切都是对象,但也可以是其他东西。例如,字符串是一个对象,但它也是一个字符串:) 可以说,该运算符返回最具体类型的名称,而不是最通用的类型。这应该可以解释为什么 typeof Function 是“函数”。至于
constructor
属性,构造函数是一个函数,在创建对象时由操作符new
调用。它始终是一个函数,无论对象本身是Object
、Function
还是其他东西。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 whytypeof Function
is "function".As for the
constructor
property, the constructor is a function that is invoked by the operatornew
when an object is created. It is always a function, regardless of whether the object itself isObject
,Function
or something else.console.log(typeof Function);
显示该对象的类型为Function
而不是object
。给你一个例子:
可以松散地翻译为
,因此,
在这两个例子中的任何一个中执行,都会产生相同的结果......
这就是为什么 Javascript 中的每个函数都是一个 Function 对象。
javascript 对象/函数上的
.constructor
、.prototype
、.toString
都是各自对象中的函数,因此为什么输出为“功能”。这是基于 ECMA-262 3rd版本 - 1999 年 12 月
希望这会有所帮助。
The
console.log(typeof Function);
shows that the object is of typeFunction
and notobject
.To give you an example:
can be loosely translated as
and thus, doing
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.
参考 每个 JavaScript 对象都是一个函数吗?
显示
也
显示
并
显示
reference Is every JavaScript Object a function?
displays
also
displays
and
displays