简体   繁体   English

构造函数:运行时“公共”属性位于内存中的位置

[英]constructor function: where "public" properties located in memory on run-time

Let's create a simple constructor function:让我们创建一个简单的构造函数:

function User(firstName) {
    this.firstName = firstName;
    this.sayHi = function () {
        console.log('Hi, my name is ' + this.firstName);
    };
}

It obvious that we can't write code like this:很明显,我们不能写这样的代码:

console.log(User.firstName); //undefined
console.log(User.sayHi);     //undefined

Let's draw how User constructor function object located in memory on run-time:让我们绘制 User 构造函数对象在运行时如何位于内存中:

______________
|              |
|    User      |
|______________|
|  prototype   |
|______________|
|     name     |
|______________|
|    length    |
|______________|
|  arguments   |
|______________|

So, we know that prototype, name, length and arguments properties are default for any function object.所以,我们知道原型、名称、长度和参数属性是任何函数对象的默认属性。 In fact, that there is no fields such firstName and sayHi in function object.事实上,函数对象中没有 firstName 和 sayHi 等字段。

So the question is: Where in memory located properties "firstName" and "sayHi", until interpreter invoke code like this:所以问题是:内存中属性“firstName”和“sayHi”的位置,直到解释器调用这样的代码:

var user = new User('Bob'); 

Thanks in advance提前致谢

So the question is: Where in memory located properties "firstName" and "sayHi", until interpreter invoke code like this:所以问题是:内存中属性“firstName”和“sayHi”的位置,直到解释器调用这样的代码:

var user = new User('Bob'); var user = new User('Bob');

In your example, there are no properties firstName and sayHi UNTIL you create a new User object.在您的示例中,没有属性firstNamesayHi直到您创建一个新的 User 对象。 The constructor of your User object creates those properties on the object that is created by the new operator when it assigns them a value. User对象的构造函数在new运算符为它们分配值时创建的对象上创建这些属性。 At that moment, those properties are created.在那一刻,这些属性被创建。

It's no different than if I have:这与我有以下没有什么不同:

var x = {};
x.foo = "Hello";

The 'foo' property was automatically created when I assigned to it. 'foo' 属性是在我分配给它时自动创建的。 Before I assigned to it, the property did not exist.在我分配给它之前,该属性不存在。 Same in your object.在你的对象中也是如此。

Now, if you had used the .prototype when defining your object, then it would have been a different story (the properties would have been defined in the prototype and automatically become part of the new object), but the way you defined your new object those properties did not exist before the object was created and the constructor code ran.现在,如果您在定义对象时使用了.prototype ,那么情况会有所不同(属性将在原型中定义并自动成为新对象的一部分),但是您定义新对象的方式在创建对象和运行构造函数代码之前,这些属性不存在。


You could prove it to yourself with this code:您可以使用以下代码向自己证明这一点:

function User(firstName) {
    console.log(this.hasOwnProperty("firstName"));
    this.firstName = firstName;
    this.sayHi = function () {
        console.log('Hi, my name is ' + this.firstName);
    };
}

http://jsfiddle.net/jfriend00/qfat6/ http://jsfiddle.net/jfriend00/qfat6/


Here's the ultimate example of a dynamically created property that is not known ahead of time.这是预先未知的动态创建属性的最终示例。

var obj = {};
var propName = window.prompt("Enter property Name");
if (propName) {
    var propValue = window.prompt("Enter property Value");
    if (propValue) {
        obj[propName] = propValue;
    }
}

If the user enters, "age" in the first prompt and "45" in the second prompt, then obj will have a property named age with a value of "45" .如果用户在第一个提示中输入“age”,在第二个提示中输入“45”,则 obj 将具有一个名为age且值为"45"的属性。 That property did not exist before this code ran and could not have been known in advance.该属性在此代码运行之前不存在,并且无法提前知道。 It was simply created upon demand when the code ran that asked for it to be created.当运行要求创建它的代码时,它只是根据需要创建的。 Assigning to a non-existent property of an object in javascript just creates that property.在 javascript 中分配给一个对象的不存在的属性只会创建该属性。


You've asked more questions about how/where code is stored.您已经询问了更多关于代码存储方式/位置的问题。 I can explain just a little bit more on that topic, but the details or how things are stored in memory in javascript is entirely implementation specific.关于该主题,我可以多解释一点,但是细节或事物在 javascript 中如何存储在内存中完全是特定于实现的。 It is not like C or C++ where an object has a very specific memory representation.它不像 C 或 C++ 那样对象具有非常具体的内存表示。 An interpreted language like Javascript does not have that.像 Javascript 这样的解释型语言没有这个。

When the code is initially parsed, a Function object is created for the User function.最初解析代码时,会为 User 函数创建一个Function对象。 That Function object then has a number of properties, which would include a reference to the code that goes with it.然后,该Function对象具有许多属性,这些属性将包括对其附带的代码的引用。 How and where the Javascript engine stores that code is entirely up to the specific Javascript implementation and likely varies depending upon how the implementation chooses to manage memory. Javascript 引擎存储代码的方式和位置完全取决于特定的 Javascript 实现,并且可能会根据实现选择管理内存的方式而有所不同。 It is not specified in a standard and there is no need for the code storage to be implemented the same in different implementations.它没有在标准中指定,并且不需要在不同的实现中实现相同的代码存储。 The only thing that must be true is that when that function is called that the interpreter knows how to retrieve the code for that Function object and execute it.唯一必须为真的是,当调用该函数时,解释器知道如何检索该 Function 对象的代码并执行它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM