JavaScript - 作为对象的函数 [英] JavaScript - Functions as objects
问题描述
如果我能理解下面的问题,那么JavaScript中的对象的概念就可以。我搜查了一下,看看javascript引擎,试图找到答案,但我目前找到的解释没有任何解释在我脑海里...
一个像下面这样的对象可以理解为构造的散列图类型。
var person = {
firstName :John,
姓氏:Doe,
年龄:50,
eyeColor:blue
};
然而,这也是一个对象,我陷入困境:
var name = function(){
alert('name');
}
-
这个函数是否存储?
-
函数的哈希映射中的语句是否按执行顺序排列?所以每一个属性都会在另一个之后被调用?
-
person
的属性是firstName ,
lastName
,age
和eyeColor
,每个持有相应的原始值 -
person
的内部插槽是(除其他外):
-
[[prototype]]
,指向Object.prototype
-
-
name
的属性是name
,prototype
和长度
(通常用于Function
个例) > name 的内部插槽是(其中包括): -
[[prototype]]
,指向
Function.prototype
-
[[ECMAScriptCode]]
,指向函数
[[Environment]]
的代码,指向创建闭包的范围
In terms of memory, how is this function stored?
Are the statements inside the "hash map" of a function layed out in an execute order? So each property is called upon after the other?
person
's properties arefirstName
,lastName
,age
andeyeColor
, each holding the respective primitive valueperson
's internal slots are (amongst others):[[prototype]]
, pointing toObject.prototype
name
's properties arename
,prototype
andlength
(as typical forFunction
instances)name
's internal slots are (amongst others):[[prototype]]
, pointing toFunction.prototype
[[ECMAScriptCode]]
, pointing to the code of the function[[Environment]]
, pointing to the scope the closure was created in
我可能错过了某些东西或者想象错误。
谢谢。
PS
问题2,
说我有一个if语句在我的函数中...将被存储在一个属性可以通过其属性之一访问?
函数对象 声明:这只是它的行为方式,引擎可能会执行此操作然而他们想要。尽管如此,它仍然可以作为一种心理模型,并且理解对象在公共可见属性下面有一层是很重要的。 The concept of functions being objects in JavaScript would be ok with me if I could understand the following question. I have searched around and looked into the javascript engine to try and find the answer, but no explanation I've found so far sits well in my mind... An object like the one below is understandably layed out in a hash map type of construct. However, to say this is also an object is where I get stuck:
I'm probably missing something or visualising something wrong. Thanks. P.S To clear up question 2, Say I have an if statement inside my function... will that be stored in a property accessible through one of its properties? Every object in javascript has properties (key-value pairs identified by strings or symbols) and it has internal slots. The function object Disclaimer: That's only how it behaves, engines may implement this however they want. Still, it serves well as a mental model, and it's important to understand that objects have a layer below the publicly visible properties. 这篇关于JavaScript - 作为对象的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
name
以与
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
};
var name = function () {
alert ('name');
}
name
is stored in the same format as the person
object, but their internal slots differ.