Reference的Base类型有哪些?不同场景下的Reference结构问询
Great question! Let’s break down the internal Reference type from ECMAScript spec clearly, covering all possible Base types and their corresponding structures with real examples.
First, a quick recap: every Reference (an engine-internal concept) has three fixed components:
Base: The "container" holding the referenced value (supports multiple types we’ll cover)ReferencedName: The name/key of the value being referenced (string or Symbol)StrictReference: Boolean flag indicating if the reference exists in a strict mode context
1. Environment Record (Your var a Example is Correct!)
This is the most common case for variables declared with var, let, const, function declarations, or parameters. Environment Records are the engine’s way of tracking variable bindings across scopes (global, function, block).
For your example:
var a;
The corresponding Reference structure is exactly what you proposed:
Reference = { Base: Environment Record, // Specifically the global Environment Record here ReferencedName: "a", StrictReference: false // Non-strict mode is default }
If this code ran in a strict mode block (with 'use strict';), StrictReference would be true.
2. undefined (Unresolvable Reference)
This happens when you reference a name that doesn’t exist in any scope (no variable binding, no global property). For example, accessing an undeclared variable b in non-strict mode:
console.log(b); // Throws ReferenceError, but the Reference exists briefly
The Reference structure here is:
Reference = { Base: undefined, ReferencedName: "b", StrictReference: false }
Note: In strict mode, accessing an unresolvable reference throws an error immediately, but the Reference still uses Base: undefined under the hood before the error is triggered.
3. Object
This applies when accessing properties of any object (arrays, functions, built-in objects like window in browsers). For example:
const user = { username: "jdoe" }; user.username;
The Reference for user.username looks like this:
Reference = { Base: user, // The actual object instance ReferencedName: "username", StrictReference: false }
Array elements follow the same rule: arr[3] would have Base: arr and ReferencedName: "3".
4. Boolean, String, Number (Primitive Values)
Even though primitives aren’t objects, the spec lets them act as the Base of a Reference when accessing their built-in properties. The engine temporarily wraps the primitive in a wrapper object (Boolean, String, Number) to resolve the property, but the Reference’s Base remains the primitive itself.
For example:
"hello world".length;
The Reference structure is:
Reference = { Base: "hello world", // The string primitive, not the wrapper object ReferencedName: "length", StrictReference: false }
Similarly, true.toString() would have Base: true (boolean primitive) and ReferencedName: "toString".
Quick Notes
- References are internal — you can’t manipulate them directly in regular JS, but understanding them explains behaviors like
delete,typeof, and scope resolution quirks. - The
StrictReferenceflag changes behavior: in strict mode, assigning to a Reference withBase: undefined(undeclared variable) throws an error, whereas non-strict mode creates a global property instead.
内容的提问来源于stack exchange,提问作者MaximPro

