Javascript 每个对象都是一个函数,每个函数都是对象——哪个是正确的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3449596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Every Object is a function and every function is Object - Which is Correct?
提问by gekrish
I was reading this link JavaScript_syntax
我正在阅读此链接JavaScript_syntax
This seems to be cyclic - that every function is an Object and every Object itself is a function. Which is the atomic one? Can someone explain in a better way?
这似乎是循环的——每个函数都是一个对象,每个对象本身都是一个函数。哪个是原子的?有人可以以更好的方式解释吗?
回答by Aaron Digulla
Anything that is not a primitive type (undefined, null, number, string, boolean) is an object (or an instance) in JavaScript. That means
functioninherits fromobject.Object instances can contain more instances which can be functions. That's what we call a "method" (since it has an automatic
thisvariable).Since you can't "call" every Object instance, not every object is a function.
任何不是原始类型(未定义、空值、数字、字符串、布尔值)的东西都是 JavaScript 中的对象(或实例)。这意味着
function继承自object.对象实例可以包含更多可以是函数的实例。这就是我们所说的“方法”(因为它有一个自动
this变量)。由于您不能“调用”每个 Object 实例,因此并非每个对象都是一个函数。
回答by Rohit
I think this concept is often misunderstood.
我认为这个概念经常被误解。
A utility to visualize JS types relationship http://jstype.herokuapp.com/#/home
一个可视化 JS 类型关系的实用程序http://jstype.herokuapp.com/#/home
Javascript Data Types
Javascript 数据类型
- Primitive types - numbers, strings, booleans, null and undefined.
- All non-primitive types are object:
- 原始类型 - 数字、字符串、布尔值、空值和未定义。
- 所有非原始类型都是object:
var foo = { };
var foo = [1, 2, 3];
var foo = function abc() { return "hello world"; };
var foo = new Number(30);
var foo = new String("Hello World");
var foo = new Boolean(true);
var foo = new RegExp(/[foo]+/);
// All 'foo` are object.
All primitive types have a corresponding Constructor Functionwiz.
Array, Number, String, Boolean, RegExp. As all functions are objects, they are objects too. So we can call them Constructor Function Objects.Most of the non-primitive type has
prototypeproperty where all inherited stuff lives. Math doesn't have prototype.All objects inherit from
Object.prototypewhich inherits fromnull.object <- Object.prototype <- nullAll native functions inherit from Function.prototype which inherits from Object.prototype.
function <- Function.prototype <- Object.prototype <- nullArrays inherit from
Array.prototypewhich inherits fromObject.prototype.array <- Array.prototype <- Object.prototype <- null
所有原始类型都有相应的构造函数wiz。
Array, Number, String, Boolean, RegExp. 由于所有函数都是对象,因此它们也是对象。所以我们可以称它们为构造函数对象。大多数非原始类型具有
prototype所有继承的东西都存在的属性。数学没有原型。继承自 的所有对象都
Object.prototype继承自null.object <- Object.prototype <- null所有原生函数都继承自 Function.prototype,后者继承自 Object.prototype。
function <- Function.prototype <- Object.prototype <- null数组
Array.prototype继承自Object.prototype。array <- Array.prototype <- Object.prototype <- null
Must read MDN: Inheritance and prototype chain
To get confused Stackoverflow: prototype in JavaScript
Stack Overflow: Function prototype explained
必须阅读MDN:继承和原型链
弄糊涂Stackoverflow:JavaScript 中的原型
Stack Overflow:函数原型解释
回答by Daniel Vandersluis
Every function is an object. Objects can contain functions (methods) but an object is not necessary a function.
每个函数都是一个对象。对象可以包含函数(方法),但对象不一定是函数。
回答by fredrik
Also Functionis always a property of an object.
也Function始终是 的属性object。
This mean that all functions in JavaScript is always bound to an object. If you don't specify an object to bind a function to it's bound to the window object (Also called global functions)
这意味着 JavaScript 中的所有函数总是绑定到一个对象。如果你没有指定一个对象来绑定一个函数到它绑定到窗口对象(也称为全局函数)
..fredrik
..弗雷德里克
回答by jasongetsdown
As others have said, functions are objects that can be passed around by reference like other js objects. Not all objects are functions, only those that are declared as such.
正如其他人所说,函数是可以像其他 js 对象一样通过引用传递的对象。并不是所有的对象都是函数,只有那些被声明为函数的对象。
You will often see methods declared like so:
你经常会看到这样声明的方法:
var myFunc = function(foo, bar) {
...
};
This is to reinforce the fact that the mthod is a function object and as such it is a property of the object where it is defined, just like any other variable you might define with var.
这是为了强化这样一个事实,即 mthod 是一个函数对象,因此它是定义它的对象的属性,就像您可能使用var.
This is the foundation of the most important feature in javascript, closure.
这是 javascript 中最重要的特性closure 的基础。
回答by Jose Diaz
It would be better to say that in JavaScript everything can be treated as an object, that includes primitive types as well as functions types; what the JavaScript interpreter does is that it automatically promotes your primitives and functions to their object wrapper types when you interact with them.
最好说在 JavaScript 中一切都可以被视为对象,包括原始类型和函数类型;JavaScript 解释器所做的是,当您与它们交互时,它会自动将您的原语和函数提升为它们的对象包装器类型。
There is also a Function object, an a number of equivalent Wrappers for the other primitives in JavaScript, that means that you can even call methods on functions instances, like:
还有一个 Function 对象,它是 JavaScript 中其他原语的许多等效包装器,这意味着您甚至可以在函数实例上调用方法,例如:
myFunction(someArg).call(this)
That being said, not every object is in fact a function.
话虽如此,并非每个对象实际上都是一个函数。
回答by James Curran
Every function is an Object.
每个函数都是一个对象。
I'm not an javascript expert, but I cannot see how every Object is a function. (I can see how every object could bea function, but that's different)
我不是 javascript 专家,但我看不出每个对象都是一个函数。(我可以看到每个对象都可以是一个函数,但那是不同的)
回答by Ekim
Quoting from Working with Objects - MDN Docs
section Using Object Initializerslast paragraph:
引用自使用对象 - MDN 文档
部分使用对象初始值设定项的最后一段:
"In JavaScript 1.1 and earlier, you cannot use object initializers. You can create objects only using their constructor functions or using a function supplied by some other object for that purpose. See Using a Constructor Function."
“在 JavaScript 1.1 及更早版本中,您不能使用对象初始值设定项。您只能使用对象的构造函数或使用其他对象为此目的提供的函数来创建对象。请参阅使用构造函数。”
meant that all objects WERE functions! Specifically, upon evaluation, instances or instantiations of functions.
意味着所有对象都是函数!具体来说,在评估、实例或函数实例化时。
Literally, ALL objects of that vintage were created syntactically with constructs like:
从字面上看,那个年份的所有对象都是用如下结构在句法上创建的:
"newobj = new constructFunctor(arg1,arg2){this.prop1=arg1 /* etc */}(val1,val2)"
AND in the string that makes the object "newobj" there is the word "constructFunctor", the name of a function. The statement is intentionally quoted to impress the fact that it must be eval()'d to be executed. Prior to execution "newobj" is "equated to" a function because the statement MUST have one and "is" one by virtue of "constructFunctor"'s literal existence to define newobj's value upon execution. The quoting, and not, is very intentional in order to elucidate this abstraction. However, because JavaScript DOES have an evalfunction, this abstraction is in fact intentionally incorporated into the JavaScript language.
AND 在使对象“ newobj”的字符串中有单词“ constructFunctor”,即函数的名称。故意引用该语句是为了给人留下这样一个事实,即它必须eval()被执行。在执行之前,“ newobj”“等同于”一个函数,因为语句必须具有一个并且“是”一个,因为“ constructFunctor”的字面存在来定义newobj执行时的值。为了阐明这种抽象,引用而不是引用是非常有意的。然而,因为 JavaScript 确实有一个eval函数,所以这种抽象实际上是有意合并到 JavaScript 语言中的。
The legacy of this is still fundamental to JavaScript, though some syntactic short cuts have been added as "object initializers" using the shorthand notation like: "no={}". That the paragraph quoted above is still resident in the current documentation IS significant for the very reasons noted.
这个遗留问题仍然是 JavaScript 的基础,尽管使用速记符号添加了一些语法捷径作为“对象初始值设定项”,例如:“ no={}”。出于上述原因,上面引用的段落仍然存在于当前文档中非常重要。
Furthermore, JavaScript also exemplifies the fundamental paradigms of Functional Programming. This defines everything as a function using the abstractions of Recursive Function Theory and the Lambda Calculus! For instance 0(), 1(), 2(), ... are the constant nonary functions better known as 0,1,2,3, ...
此外,JavaScript 还体现了函数式编程的基本范式。这使用递归函数理论和 Lambda 演算的抽象将一切定义为函数!例如 0(), 1(), 2(), ... 是常被称为 0,1,2,3, ...
JavaScript is completely consistent with a Functional Programming Style approach rather than the common OOPS (pun intended Object Oriented Programming Style).
JavaScript 完全符合函数式编程风格方法,而不是常见的 OOPS(双关语意为面向对象编程风格)。
回答by AllenMa
Just for supplementary to Aaron Digulla's answer. In javascript not every object is a function. But Object,Array,String and many other built-in objects are functions that used with new operator to create object. I think this is what confused most people.
仅作为 Aaron Digulla 答案的补充。在 javascript 中,并不是每个对象都是一个函数。但是 Object、Array、String 和许多其他内置对象是与 new 运算符一起使用来创建对象的函数。我想这是大多数人困惑的地方。
回答by godseyeview
javascript everything is a hashtable. Ryan Dahl said this in one of his talks. thats what json is also; a hashtable definition. thats why u can access an object using array notation or object property notation. the value of the hashtable can be a function as well which is a hashtable. or rather an associative array type Object in the console u get { [native code] } which is a hashtable
javascript 一切都是一个哈希表。瑞安达尔在他的一次谈话中说到了这一点。这就是 json 也是;哈希表定义。这就是为什么您可以使用数组表示法或对象属性表示法访问对象的原因。哈希表的值也可以是一个函数,它是一个哈希表。或者更确切地说,控制台中的关联数组类型对象你得到 { [native code] } 这是一个哈希表

