St.Antario
St.Antario

Reputation: 27425

Identifier resolution and simple assignment

Let we have a simple script:

obj=new String('str');//1
obj.length;//2

At line 1 we have a simple assignment which evaluated as follows:

  1. Evaluate PrimaryExpression: Identifier (obj in my case). (undefined, obj, false) will be returned as result.
  2. Evaluate NewExpression new String('str');. Denote result of evalution by rref
  3. GetValue(rref)
  4. Putting value getting at step 3 to reference getting at step 1.
  5. Return result of step 4.

At this line obj-->'str' bindings doesnt add to environment record of any execution contex. But when obj PrimaryExpression:Identifier will be evaluated as ('str', obj, false) type Refernce and we can get property of 'str' by this reference.

Question:

Why PrimaryExpression:Identifier obj at line 2 will be evaluated as ('str', obj, false)?

Upvotes: 0

Views: 39

Answers (1)

satchmorun
satchmorun

Reputation: 12477

This is an unusual sort of question for SO, but interesting nonetheless.

The answer to your question is that it gets added to the global execution context (assuming this script is the whole program).

See the ecma spec section regarding Programs.

Step 3 sets up an execution context for global code, and it's this context in which obj is initially referenced, and which the PutValue operation sets the binding of in your step 4 above.

Upvotes: 1

Related Questions