About Reference type
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
- Let baseReference be the result of evaluating MemberExpression.
- 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.
I want to ask some questions about Reference in ECMAScript.
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)
Yeah, difference in Base value. Can simple string such as "someString" be Base?
My question is complex. But type Reference is too hard for understanding without additional explanation.