本文介绍了JavaScript - 作为对象的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我能理解下面的问题,那么JavaScript中的对象的概念就可以。我搜查了一下,看看javascript引擎,试图找到答案,但我目前找到的解释没有任何解释在我脑海里...



一个像下面这样的对象可以理解为构造的散列图类型。

  var person = {
firstName :John,
姓氏:Doe,
年龄:50,
eyeColor:blue
};

然而,这也是一个对象,我陷入困境:

  var name = function(){
alert('name');
}




  1. 这个函数是否存储?


  2. 函数的哈希映射中的语句是否按执行顺序排列?所以每一个属性都会在另一个之后被调用?



  3. 我可能错过了某些东西或者想象错误。



    谢谢。

    PS



    问题2,

    说我有一个if语句在我的函数中...将被存储在一个属性可以通过其属性之一访问?

    解决方案

    javascript中的每个对象都有属性(由字符串或符号标识的键值对),并且它具有。

    函数对象 name 以与 person 对象相同的格式存储,但它们的内部插槽不同。




    • person 的属性是 firstName , lastName , age 和 eyeColor ,每个持有相应的原始值

    • person 的内部插槽是(除其他外):




      • [[prototype]] ,指向 Object.prototype


    • name 的属性是 name , prototype 和长度(通常用于)

    • > name 的内部插槽是(其中包括):


    • [[prototype]] ,指向 Function.prototype

    • [[ECMAScriptCode]] ,指向函数
    • [[Environment]] 的代码,指向创建闭包的范围





    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.

    var person = {
        firstName:"John",
        lastName:"Doe",
        age:50,
        eyeColor:"blue"
    };
    

    However, to say this is also an object is where I get stuck:

    var name = function () {
        alert ('name');
    }
    
    1. In terms of memory, how is this function stored?

    2. 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?

    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 name is stored in the same format as the person object, but their internal slots differ.

    • person's properties are firstName, lastName, age and eyeColor, each holding the respective primitive value
    • person's internal slots are (amongst others):

      • [[prototype]], pointing to Object.prototype
    • name's properties are name, prototype and length (as typical for Function instances)

    • name's internal slots are (amongst others):
      • [[prototype]], pointing to Function.prototype
      • [[ECMAScriptCode]], pointing to the code of the function
      • [[Environment]], pointing to the scope the closure was created in

    这篇关于JavaScript - 作为对象的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:29