문제

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      |
|______________|
|  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.

So the question is: Where in memory located properties "firstName" and "sayHi", until interpreter invoke code like this:

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:

var user = new User('Bob');

In your example, there are no properties firstName and sayHi UNTIL you create a new User object. 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. 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. 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.


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/


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". 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.


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. It is not like C or C++ where an object has a very specific memory representation. An interpreted language like Javascript does not have that.

When the code is initially parsed, a Function object is created for the User function. That Function object then has a number of properties, which would include a reference to the code that goes with it. 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. 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top