About Reference type

# Maxim Vaarwel (7 years ago)

I want to ask some questions about Reference in ECMAScript.

  1. What data is of type Reference in the specification?
  2. The specification says that: "The base value component is either undefined, an Object, a Boolean, a String, a Symbol, a Number, or an Environment Record". Almost everything is listed here. What in this case will not be the Reference type? And I would like to see examples of code with each base value. For example:
/// Definition variable in javascript code (example of javascript code)
var a;
/// Reference structure in engine
Reference = {
    Base: Environment Record, /// It's one of other cases
    ReferencedName: "a",
    StrictReference: false
}
/// Is there a difference between the two expressions?
"someString".toString; /// ƒ toString() { [native code] }
/// and
var storage = "otherString";
storage.toString; /// ƒ toString() { [native code] }

The question is how to calculate these expressions? How will the Reference type fields be filled?

What how I think (maybe it's wrong, I don't know)

/// "someString".toString;
Reference = {    
    Base: "someString",
    ReferencedName: "toString",     
    StrictReference: false
}
/// storage.toString;
Reference = {
    Base: storage,
    ReferencedName: "toString",
    StrictReference: false
}

Yeah, difference in Base value. Can simple string such as "someString" be Base?

  1. In the code, in the global code there is a variable bar (declared, bar - example of variable). We can address to it as window.bar orbar ... and here the most interesting. With window.bar what is base? And when bar? We know that declarations in the global code are the Object Records Environment, and if so, base will be appeal to the bar Records Environment. But appeal to the window.bar, base will be a window - type(Object). And here it is not clear. If this is an object environment, then why call through dot will be a window object, but not an object environment?
  2. If inside of object we work with property object, the property of this object will have a Declarative Records Environment or Object Records Environment.

My question is complex. But type Reference is too hard for understanding without additional explanation.

# Logan Smyth (7 years ago)

What data is of type Reference in the specification?

If you search for "value of type Reference" in the spec, I think it finds all of the places where references are created.

References come from references to variable names, or from things like property accesses like foo.bar or foo['bar'] (also if the key is an expression that was evaluated). You can see here for instance www.ecma-international.org/ecma-262/8.0/#sec-property-accessors-runtime-semantics-evaluation

Return a value of type Reference whose base value component is bv, whose

referenced name component is propertyNameString, and whose strict reference flag is strict.

The specification says that: "The base value component is either

undefined, an Object, a Boolean, a String, a Symbol, a Number, or an Environment Record". Almost everything is listed here. What in this case will not be the Reference type?

That list is the list of types that can be the based, not the list of types that are references. All of the standard JS types, and environment records may be the "base" of a reference.

Yeah, difference in Base value. Can simple string such as "someString" be

Base?

"someString" would be the Base value in both of those examples. For storage.toString, the value of storage will be evaluated first, so when the reference is created with the string value as the base. You can see that in the spec section I linked above

  1. Let baseReference be the result of evaluating MemberExpression.
  2. Let baseValue be ? GetValue(baseReference).

So GetValue(baseReference) will get the actual value of storage and then it will be used as the base string value.

With window.bar what is base?

window would be evaluated to the Window object, which would be the Base value.

And when bar?

This will run www.ecma-international.org/ecma-262/8.0/#sec-getidentifierreference which will recurse up through the various nested environments until it either finds one with a bar declaration, or finds none. Assuming window.bar exists and you're in the global scope, it would set the global environment record as the Base for the reference.

If this is an object environment, then why call through dot will be a

window object, but not an object environment?

I'm not sure I follow here. Where would an object environment come up in that case?

If inside of object we work with property object, the property of this

object will have a Declarative Records Environment or Object Records Environment.

I'm not sure I understand what you're asking here.