简体   繁体   English

javascript函数和javascript对象之间的主要核心区别是什么?

[英]What is the main core difference between a javascript function and javascript object?

当您对这两种类型都执行“ typeof”操作时,您会获得“ function”(函数)和“ object”(对象),但这不是使函数成为特殊对象,如果是的话,将函数与变量区分的属性是什么?宾语?

When you do a "typeof" on both of these types you get "function" for functions and "object" for objects, 当您对这两种类型都执行“ typeof”操作时,对于函数,您将获得“ function”;对于对象,您将获得“ object”,

First of all, the specification for typeof is basically just a lookup table, where it says "if the value is a function object, return the string "function" ). So it doesn't provide the real data type of the value (which would be object for functions). 首先, typeof的规范基本上只是一个查找表,其中说“如果值是函数对象,则返回字符串"function" )。因此它不提供值的实际数据类型(将是功能的对象)。

but isn't it so that functions are special objects 但是不是吗,所以功能是特殊的对象

Yes. 是。 Functions are so called callable objects. 函数就是所谓的可调用对象。

Objects have, in addition to "normal" properties, so called "internal" properties . 除了“正常”属性外,对象还具有所谓的“内部”属性 You can think of these as some kind of internal state, that needs to be maintained for the object to work correctly, but that is not accessible in user code. 您可以将它们视为某种内部状态,需要维护该状态才能使对象正常工作,但是在用户代码中不可访问。

Some of these internal properties make certain objects special because not every object has them. 这些内部属性中的某些使某些对象变得特殊,因为并非每个对象都有它们。 One of them is [[Call]] (internal properties are denoted with double brackets), which contains code in some implementation-specific format. 其中之一是[[Call]] (内部属性用双括号表示),其中包含某些实现特定格式的代码。

When you call a value (ie foo() ) the interpreter first checks whether the value is an object and then checks whether it has an intern [[Call]] property. 当您调用一个值 (即foo() )时,解释器首先检查该值是否为对象,然后检查其是否具有intern [[Call]]属性。 If yes, then the code stored in that property is executed. 如果是,则执行存储在该属性中的代码。

Here is a very rough example of how this could look like internally: 这是一个非常粗糙的例子,说明内部情况:

 // Simulates a function call implementation, ie what happens when // you do `foo()` function call(value) { if (typeof value !== "object") { throw new Error('Not an object'); } if (!value["[[Call]]"]) { throw new Error('Not a function'); } return eval(value["[[Call]]"]); } // Simulated function object that has a name ("normal" property) // and the internal property [[Call]]. // This would be the internal representation for something like // function func() { // console.log('some code'); // } var func = { name: "func", "[[Call]]": "console.log('I\\\\'ma function!');", }; call(func); 

Side note: If you know Python, then this concept should be familiar to you, because Python allows you to make arbitrary objects callable by implementing __call__ . 旁注:如果您了解Python,则应该熟悉此概念,因为Python允许您通过实现__call__来使任意对象可__call__

In addition to [[Call]] there is also [[Construct]] . 除了[[Call]] ,还有[[Construct]] We actually distinguish between callable and constructable functions. 我们实际上区分了可调用函数和 构造函数。 Constructable functions are those that can be invoked with new . 可构造函数是可以用new调用的函数。 Functions created via function ... are both callable and constructable. 通过function ...创建的function ...既可调用又可构造。 Arrow functions are only callable, functions created via class ... are only constructable. 箭头函数仅可调用,通过class ...创建的函数仅可构造。 And this distinction is made based on whether [[Call]] or [[Construct]] or both are set. 根据是否设置了[[Call]][[Construct]]或两者都进行了区分。

if so what are the properties that differentiate a function from an object 如果是的话,区分功能和对象的属性是什么?

In addition to these special internal properties, function objects also have all properties defined on Function.prototype : 除了这些特殊的内部属性外,函数对象还具有在Function.prototype定义的所有属性:

console.dir(Function.prototype);
// etc

(and Function.prototype "extends" Object.prototype ) which is where .call and .apply are defined, but these alone do not make functions special. (和Function.prototype “扩展” Object.prototype ),这就是.call.apply的定义,但这些不能单靠职能特殊。 The internal [[Call]] property is what makes them special. 内部的[[Call]]属性使它们与众不同。


The same applies to other built-in "classes" such as Array , Date or RegExp . 这同样适用于其他内置的“类”,例如ArrayDateRegExp All instances of these classes have additional methods/properties that are defined on Array.prototype , Date.prototype , etc. 这些类的所有实例都具有在Array.prototypeDate.prototype等上定义的其他方法/属性。

Functions (which are first-class objects) can be called , while other objects cannot. 函数(属于一流对象) 可以被调用 ,而其他对象则不能。

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions for more detail. 有关更多详细信息,请参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions

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

相关问题 在 javascript 中导出 function 或 object 有什么区别? - What is the difference between exporting a function or an object in javascript? JavaScript对象和函数之间的区别 - Difference between javascript object and function JavaScript Function()和JavaScript对象之间的区别 - Difference between a JavaScript Function() and a JavaScript object 在javascript对象中创建函数的不同方法有什么区别? - What is the difference between different ways to create a function inside javascript object? Javascript:使用的区别是什么? 和:用于在函数或对象中指定变量的运算符? - Javascript: What is the difference between usage of . and : operators for specifying a variable in a function or object? 这两种在JavaScript函数中利用arguments对象的方式有什么区别? - What is the difference between these two ways to leverage the arguments object in a JavaScript function? javascript中函数和对象的本质区别是什么? - What's substantive difference between function and Object in javascript? 在javascript中,对象和命名空间之间有什么区别? - In javascript, what is the difference between an object and a namespace? JavaScript Object创建方法有什么区别? - What is the difference between JavaScript Object creation methods? JavaScript中两个对象之间的区别是什么 - what is difference between two object in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM