11

I've been trying to understand how this value is set in javascript, and found ECMAScript Language Specification pretty much helpful. I was reading section 8.7 reference specification type and found that reference in ECMAScript is made of 3 component, base value, referenced name, strict reference flagto understand section 11.2.3.

I can assume what are referenced name and strict reference flag from their name, but i don't understand what is the base value. The document says that base value is either undefined, String, Boolean, Number and Object, but it does not say how it is set and what it is. I am guessing it is something similar to context object. Could anyone explain?

ringord
  • 908
  • 3
  • 11
  • 27
  • like @shashank points out, `this` inherits the `typeof` from whatever object it is referencing. i believe all variables in JS are of `Prototype Object` – RenaissanceProgrammer Mar 30 '15 at 18:14
  • @Shashank: The value of `this` is not transformed, it's not a special object or type. `this` is special because the value is directly referenced from the execution context, not the corresponding environment record. The value however is just like any other value. The question doesn't actually have to do anything with `this`. – Felix Kling Mar 30 '15 at 18:30
  • 1
    http://perfectionkills.com/know-thy-reference/ – kangax Apr 02 '15 at 11:12

1 Answers1

6

Yes, the base value is the context in which the referenced name lives.

For an object property, this would be the object (see §8.12 Object internal methods for setter/getter operations). For a variable, this would be the variable environment (§10.2.1 Environment records). For an unresolvable reference (the things that throw reference errors except when supplied to typeof), this would be undefined.

it does not say how it is set

Reference values are only constructed by very few operations:

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks for your answer. I am kinda starting to understand it. I also read [Section 10.4.3](http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3), it says if it is either `null` or `undefined`, `this` value is set to global object. So one more question, is there any case that `base value` is `null`? – ringord Mar 30 '15 at 18:43
  • 1
    Notice that the `thisBinding` has nothing to do with reference values. The `thisArg` is just a value supplied by the caller - never `null` in case of a method invocation indeed, but you could use [`.call(null)`](http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.4) as well. No, a reference is never constructed with `null` as the *base value*. `null.x` and `undefined.x` [do throw immediately](http://es5.github.io/#x9.10), and identifiers references have only `undefined` passed explicitly. – Bergi Mar 30 '15 at 18:52
  • When is the base value a `Boolean`, `Number`, or `String` ? – doubleOrt Feb 14 '18 at 10:37
  • @Taurus When you access a property on a primitive value – Bergi Feb 14 '18 at 14:33
  • @Bergi Isn't it boxed when you do that (so it is just like you are accessing it on an object) ? BTW does this section in the spec confirm that everything in JS is a reference (and that everything is by reference and not by value) ? – doubleOrt Feb 14 '18 at 15:10
  • 2
    @Taurus No, it does get boxed only when the reference is evaluated (`GetValue`), not when the reference is constructed. And No, this has nothing to do with reference-values vs primitive-values, notice that all references are evaluated to their values before a call. – Bergi Feb 14 '18 at 15:21
  • If Type(V) is not Reference, return V. When will Type(V) will not be a reference. Is it in case of literals ?? – Harish_N Oct 02 '18 at 03:11
  • @Harish_N Where are you citing this from? (No, `V` typically can be any value, not necessarily a primitive one or one constructed by a literal) – Bergi Oct 02 '18 at 07:55
  • @Harish_N The `GetValue()` algorithm is called throughout the entire spec, and in most cases not with a reference. – Bergi Oct 02 '18 at 10:04