The Reference type & this
Learn why cart.show() knows its cart but a saved copy of show doesn’t, using the spec’s Reference Records: short-lived addresses for names and properties.
- 01Read a Reference RecordName its four parts and say what each is for: where to look, what to find, strict mode, and
super. - 02Predict thisExplain why
cart.show()gets the cart asthis, but a comma call or a saved copy of the method does not. - 03Trace reads and writesFollow how JavaScript reads and writes through an address, why
typeofis safe for missing names, and howsuperworks.
A value and a place to find it
When you write cart.price, JavaScript does two small jobs. First it works out where to look. Then it reads what is there. You almost never notice the first job. This lesson is about that first job, and why it decides what this means inside a method.
const cart = { price: 3 };
console.log(cart.price);Line 1 makes a cart object that holds a price of 3. Line 2 asks for cart.price and prints 3. Just before the read, JavaScript knows two things: the object to look in (cart) and the name to look for (price).
You ask a librarian for a book. She doesn't hand you the book. She writes a slip: "Shelf B, Book 3." The slip is not the book. It only tells you where the book is. Once you have the book, you don't need the slip any more.
- In real life: The slip says Shelf B
- In JavaScript: The object to look in, like
cart - In real life: The slip says Book 3
- In JavaScript: The name to look for, like
price - In real life: You walk over and take the book
- In JavaScript: JavaScript reads the value,
3
A Reference Record is an address for a value: it says where to find the value, not what the value is. For cart.price the address is "the property price, inside cart". JavaScript works out the address first, then uses it to read or write. Your code can't see or save the address. For a method call, the address also tells JavaScript which object should become this.
This idea solves a common puzzle. Why does cart.show() know about the cart, but a saved copy of show doesn't? By the end of this lesson, you'll know the answer.
Reference Records
A postal address has parts, like a street and a house number. A Reference Record has parts too. You need two of them most:
- The base: where to start looking, like
cart. - The referenced name: what to find there, like
price.
There are two more parts. One says whether the code is in strict mode: yes or no. The other is only used by super; you'll see it near the end of the lesson. All four parts live inside the specification. You can't read them as properties on your cart.
const cart = { price: 3 };
const price = cart.price;
console.log(price);Line 1 makes the cart. Line 2 reads cart.price and keeps the number in a new variable, price. Line 3 prints 3. After line 2, price is just the number 3. It doesn't remember that it came from the cart. The address was used once, for the read, and then it was gone.
Reference Record fields:
[[Base]], [[ReferencedName]], [[Strict]], [[ThisValue]]
// Spec notation, not JavaScript syntax.This is how the specification writes the four parts. The double square brackets are just how the spec names a part. You don't need to memorize them:
- [[Base]] is where to look. It's an object like
cart, a scope's list of variables, or "unresolvable" when nothing was found. - [[ReferencedName]] is the name to find, like
price. - [[Strict]] is yes in strict mode and no otherwise.
- [[ThisValue]] is empty, except in
supercalls.
The block above is a short summary, not code you can run. The full wording is in the ECMAScript Reference Record definition.
You open your contact list, find Asha, and read her number. You need two things for that: the list to look in, and the name to find.
- In real life: Open your phone's contact list
- In JavaScript: The base: where to look, like
cart - In real life: Find the name Asha
- In JavaScript: The referenced name: what to find, like
price - In real life: Read Asha's number
- In JavaScript: Read the value through the address
- In real life: Change Asha's saved number
- In JavaScript: Write a new value through the address
The address looks different depending on what you wrote. The table below shows the four cases you'll meet. Read each row as: "when I write this, JavaScript looks here, and this is what I get."
| When you write | Where JavaScript looks | What happens |
|---|---|---|
cart.price, a property | Inside the cart object, for a property called price. | You get 3, the cart's price. A method called this way, like cart.show(), knows which object it belongs to: this is the cart. |
price, a plain variable | In the current scope, for a variable called price. | You get its value. But a function called by its plain name, like show(), has no this: there's no object in front of it. It's like asking "What's the price?" without saying the price of what. |
missingItem, a name that was never created | In the current scope, then in every scope around it. It finds nothing. | Reading it throws a ReferenceError, which means "there's no such name". |
super.show(), inside a class | In the parent class, for a method called show. | The parent's show runs, but it works on your own object: this doesn't change. |
The base, where to look, depends on what you wrote. For cart.price, the base is the cart. For a plain variable like price, the base is the list of variables in the current scope. The spec calls that list an Environment Record, and the next lesson is all about it. For a name that doesn't exist anywhere, the base is "unresolvable", which means "not found". Then JavaScript can report an error instead of guessing.
How a method call chooses this
Here is the key idea of the lesson. When you write cart.show(), JavaScript first works out the address: "show, inside cart". The call happens right after, while that address is still around. So the call can see that show came from the cart, and it uses the cart as this. A property access with a dot, like cart.show, is called a member expression.
In a busy room you say, "Asha, read me your shopping list." Asha knows "your" means her list, because you spoke to her. If you shout "Read me your shopping list!" to nobody, no one knows whose list you mean.
- In real life: Asha, read me your list
- In JavaScript:
cart.show() - In real life: Asha is the person you spoke to
- In JavaScript:
thisiscart - In real life: Shouting the request to nobody
- In JavaScript: Calling
show()on its own
const cart = {
price: 3,
show() { return this.price; },
};
console.log(cart.show());Line 1 starts the cart. Line 2 gives it a price of 3. Line 3 adds a show method that returns this.price. Line 5 calls cart.show() and prints 3. You called show through the cart, so this is the cart and this.price is 3.
show() function in both. Called from the cart, it works on the cart. Copied into its own box and called alone, it has no cart to work on.Next, here is how the specification describes this. Spec steps read like a recipe. "Let x be …" means "call the result x". A ? means "if this step goes wrong, stop and throw the error". You don't need to learn this way of writing. After each box, the same steps follow in plain words.
Let baseValue be ? Evaluation of MemberExpression.
Let propertyNameValue be ? Evaluation of IdentifierName.
Return a Reference Record whose [[Base]] is baseValue.
// Shortened from the spec's property access steps.These are the spec's property access steps for cart.show. In plain words, line by line:
- Work out the part before the dot: the
cartobject. - Work out the name after the dot:
show. - Hand back an address: look in
cart, forshow. Nothing has been read yet.
The box leaves out some details, such as private fields. And the address only exists in the spec's description; a real engine doesn't have to create it in memory.
Now the call itself. The spec calls this step EvaluateCall. In the box, ref and thisValueRef are both the address from cart.show.
Let func be ? GetValue(ref).
If IsPropertyReference(thisValueRef) is true,
let thisValue be GetThisValue(thisValueRef).
Otherwise, let thisValue be undefined.
// Shortened from the spec's EvaluateCall steps.In plain words, line by line:
- Use the address to read the function. Call it
func. - Check: does the address point to a property of an object, like
cart.show? - If yes, ask the address which object it was inside, and use that object as
this. Here, that's the cart. - If not, like a plain
show(),thisstarts asundefined.
So the call uses the address twice: once to get the function, and once to choose this. When this starts as undefined, strict code keeps it. Older non-strict code uses the global object instead.
A replay of the example code, not a view inside the JavaScript engine.
script
price: 3, show() { return this.price; },};console.log(cart.show());Press Step to walk through the replay. You'll see the cart made, the call begin, and the method receive the cart as this. The last step shows the real value the method returned. The replay runs the example code step by step; it doesn't show what happens inside the JavaScript engine.
Brackets, commas, and saved methods
Small changes in how you write a call decide whether the address survives. The first change is to put round brackets around the method, like this: (cart.show)().
const cart = {
price: 3,
show() { return this.price; },
};
const price = (cart.show)();
console.log(price);Lines 1 to 4 make the cart, the same as before. Line 5 is the new part. Line 6 prints 3.
cart.show, only groups it. The second pair, (), calls the method, as always.So line 5 has two pairs of round brackets, and they do different jobs:
- The grouping brackets, around
cart.show, only group it, like brackets in maths:(2 + 3)is still just2 + 3. They change nothing. - The call brackets,
()at the end, call the method, as always.
Because the grouping brackets change nothing, the address "show, inside cart" reaches the call. So this is still the cart, and the method returns its price, 3. The other brackets in the code, like the { } around the cart and the () after show on line 3, are the usual ones for making an object and a method.
Here is the spec's step for grouping brackets. It's only one line.
Return ? Evaluation of Expression.
// The spec's whole step for brackets.In plain words:
- Work out what's inside the grouping brackets,
cart.show, and hand back exactly that.
So if an address goes in, the same address comes out. That's why (cart.show)() still knows about the cart.
A comma is different. In (0, cart.show), the comma reads the value on its right and hands back only that value: the function. The address is lost. Here is what that does to the call.
"use strict";
const cart = {
price: 3,
show() { return this === undefined; },
};
console.log((0, cart.show)());Line 1 turns on strict mode. Lines 2 to 5 make the cart and a method that checks whether this is undefined. Line 6 looks at 0, then at cart.show, and the comma hands back only the function. Then that function is called. It prints true: the method had no cart, so this was undefined.
Here are the spec's steps for the comma, for (0, cart.show).
Let lval be ? Evaluation of Expression.
Perform ? GetValue(lval).
Let rref be ? Evaluation of AssignmentExpression.
Return ? GetValue(rref).
// The spec's steps for the comma.In plain words, line by line:
- Work out the left side:
0. - Read its value, then ignore it.
- Work out the right side:
cart.show. That gives an address. - Read the value at that address, and hand back only the value: the function. This is where the address is dropped.
Saving the method in a variable does the same: the variable holds the function, not the place it came from.
A remote works when you point it at the TV. Take it to the kitchen and press the same button: it's the same remote, but there's no TV in front of it. A saved method is the same function. It just isn't pointed at the cart any more.
- In real life: Press the remote in the living room
- In JavaScript:
cart.show()works on the cart - In real life: Carry the remote to the kitchen
- In JavaScript:
const show = cart.show - In real life: Press it; nothing changes
- In JavaScript:
show()has no cart to work on
"use strict";
const cart = {
price: 3,
show() { return this === undefined; },
};
const show = cart.show;
console.log(show());Lines 1 to 5 turn on strict mode and make the cart. Line 6 reads cart.show and saves the function as show. Line 7 calls show() by its new name and prints true. Calling a plain name is not the same as calling through the cart.
A replay of the example code, not a view inside the JavaScript engine.
script
"use strict"; price: 3, show() { return this === undefined; },};const show = cart.show;console.log(show());In this replay, watch the function get saved first and then called with no cart. Because the code is strict, this stays undefined. The replay runs the real code. It can't show the address itself, because your code can never see it.
"use strict";const cart = { price: 3, show() { return this === cart; },};console.log(cart.show());trueDot callthis is cart
The call still knows about the cart, so this is the cart.
Try each way of calling in the playground. Each button changes only the last line of code. Dot call and Brackets print true for this === cart. Comma and Saved copy print false. Reset goes back to Dot call.
The table below sums up the four ways. Read each row as: "when I call it like this, does the call still know about the cart?"
| How you call it | Does the call still know about the cart? | So this is |
|---|---|---|
cart.show() | Yes. The cart is right there in front of the method, joined by the dot. | The cart |
(cart.show)() | Yes. The grouping brackets around cart.show change nothing, like in maths. The cart is still in front of the method. | The cart |
(0, cart.show)() | No. The comma hands back just the function and leaves the cart behind. | undefined in strict code |
const show = cart.show; show() | No. The variable holds just the function, not where it came from. | undefined in strict code |
Reading and writing: GetValue and PutValue
An address can be used in two ways. GetValue uses it to read: go to the place and bring back what's there. PutValue uses it to write: go to the place and put something new there. Think of a locker. The same locker number lets you take your bag out or put a new bag in.
const cart = { price: 3 };
const price = cart.price;
console.log(price);Line 1 makes cart.price with the value 3. Line 2 goes to that place, gets 3, and keeps it in price. Line 3 prints 3. Once the read is done, price is only a number. It has forgotten where it came from.
const cart = { price: 3 };
cart.price = 4;
console.log(cart.price);Line 1 makes the cart. Line 2 puts 4 into cart.price, using the address of that property. Line 3 reads the price again and prints 4. The cart changed. The number 3 itself didn't change; it was replaced.
Here are both, shortened from the spec. In the box, V is the address and W is the new value to write.
GetValue(V): if V is not a Reference Record, return V.
If IsUnresolvableReference(V), throw a ReferenceError.
For a property reference, return ? baseObj.[[Get]](name, receiver).
PutValue(V, W): if V is not a Reference Record, throw a ReferenceError.
For a property reference, perform ? baseObj.[[Set]](name, W, receiver).
// Shortened from the spec's GetValue and PutValue.In plain words, line by line:
- Reading: if
Visn't an address, it's already a value, so hand it back. - If the address leads nowhere, throw a
ReferenceError. - For a property, ask the object for the value with that name.
receiveris the object that becomesthisif the property has a getter. - Writing: you can only write through an address. Without one, throw a
ReferenceError. - For a property, tell the object to store
Wunder that name.
The full GetValue and PutValue steps also cover variables, strict mode, and proxies. The box shows only properties.
const cart = {
get price() { console.log("reading price"); return 3; },
};
console.log(cart.price);Line 1 starts the cart. Line 2 adds a getter for price: a function that runs whenever someone reads cart.price. This one logs "reading price" and returns 3. Line 4 reads the price, so the getter runs, and then 3 is printed. So a read is not always a simple copy. Sometimes reading runs code, like a shop assistant who checks the stock room before telling you the price.
const cart = { price: 3 };
(cart.price) = 4;
console.log(cart.price);Line 1 makes the cart. Line 2 writes 4 into (cart.price). The brackets still point at the same place. Line 3 prints 4. Here too, the brackets keep the address, so the write still knows where to go.
Missing names and typeof
Sometimes the name you ask for doesn't exist. Then the base part of the address says "unresolvable": nothing was found. This is called an unresolvable Reference. Reading it throws a ReferenceError.
typeof is different. It tells you what kind of value something is, as text, like "number" or "string". It has a special rule for missing names: it gives the text "undefined" instead of throwing. So you can safely ask "does this exist?" It's like asking the front desk whether a guest is staying at the hotel, rather than knocking on a room door that isn't there.
console.log(typeof missingItem);
try {
console.log(missingItem);
} catch (error) {
console.log(error.name);
}Line 1 prints undefined. That is the text typeof gives for a missing name. Line 3 tries to read the name directly. Lines 4 and 5 catch the error and print ReferenceError. Only the direct read failed; typeof did not throw.
Here is that special rule in the spec's typeof steps.
If IsUnresolvableReference(value) is true, return "undefined".
// Shortened from the spec's typeof steps.In plain words:
- If the name leads nowhere, give back the text "undefined" straight away, without trying to read it.
This rule only covers names that were never created. A let or const used before its line runs still throws a ReferenceError, even with typeof. That waiting time before the line runs is called the temporal dead zone.
"use strict";
try {
missingItem = "tea";
} catch (error) {
console.log(error.name);
}Line 1 turns on strict mode. Line 3 tries to write to a name that doesn't exist. Lines 4 and 5 catch the error and print ReferenceError. Old non-strict code would quietly make a new global variable instead. Don't rely on that; it hides typing mistakes.
How super chooses this
Remember the fourth part of an address, the one only super uses? Here it is in action. With super, two questions get different answers: where to find the method, and which object the method should work on. super finds the method on the parent class, but the method still works on your own object. JavaScript keeps your object in that fourth part. This kind of address is called a super Reference.
You want to bake Mum's cake, so you look up the recipe in her book. But you don't drive to her house to bake it. You follow her steps in your own kitchen, with your own eggs and flour.
- In real life: Look up the recipe in Mum's book
- In JavaScript: Find
showonParent.prototype - In real life: Cook in your own kitchen
- In JavaScript:
thisstays your object - In real life: The cake ends up on your table
- In JavaScript: The parent method reads your
this.name
class Parent {
show() { return this.name; }
}
class Child extends Parent {
show() { return super.show(); }
}
console.log(new Child().show.call({ name: "Asha" }));Lines 1 to 3 make Parent's show, which returns this.name. Lines 4 to 6 make Child's show, which just calls super.show(). Line 7 runs Child's show with .call(), which lets you choose this yourself. Here this is an object named Asha, and it prints Asha. The parent's method worked on that same object.
super changes where JavaScript finds the method, not which object it works on. Parent's show() runs on your object, so it gives Asha.The spec's GetThisValue steps decide what this is. In the box, V is the address.
For a super reference, GetThisValue(V) returns V.[[ThisValue]].
Otherwise it returns V.[[Base]].
// Shortened from the spec's GetThisValue.In plain words, line by line:
- For a
superaddress,thiscomes from its fourth part, [[ThisValue]]: your own object. - For every other address,
thisis the base: the object the method was found on, like the cart.
In the example, the base of the super address is Parent.prototype, the place where the parent's methods live. That's only where show is found. [[ThisValue]] holds the Asha object, and that's who show works on. So super never makes the parent this.
The table below splits a super call into three simple questions.
| Question | Answer in the example | Why it matters |
|---|---|---|
| Where is the method found? | On the parent class, in Parent.prototype. | That's whose show runs. |
| Which method? | show, the name after super. | That's the name JavaScript looks for. |
Who is this? | The object named Asha. | So this.name inside the parent's show gives Asha. |
Getters and setters that come from a parent class work the same way. JavaScript finds them on the parent, but they work on your object. Keep this in mind when one class extends another.
Fixing a real callback
Here is where this causes real bugs. A callback is a function you hand over to be run later, for example when a button is clicked. If you hand over cart.show on its own, you hand over only the function. When the button runs it later, the cart is gone. The fix is to hand over a small arrow function that calls cart.show() itself.
"use strict";
const cart = {
price: 3,
show() { return this.price; },
};
const button = { onclick: () => cart.show() };
console.log(button.onclick());Line 1 turns on strict mode. Lines 2 to 5 make the cart and its show method. Line 6 saves a small arrow function that calls cart.show(). Line 7 runs it and prints 3. The arrow remembers the cart, and when it runs it makes a normal cart.show() call.
You're going away, and you ask a neighbour to water your plants. If you only hand over the watering can, they don't know whose plants to water. A note that says "water my balcony plants" carries the missing part: which plants.
- In real life: Hand over only the watering can
- In JavaScript:
onclick: cart.show - In real life: Your neighbour doesn't know whose plants
- In JavaScript:
thisis not the cart - In real life: Leave a note: water my balcony plants
- In JavaScript:
onclick: () => cart.show()
Want more practice with this? The this keyword and Losing this lessons show common call patterns and fixes. Arrow functions and lexical this explains why the arrow doesn't change what this is inside show. This lesson explains why all of them work the way they do.
cart.show()(cart.show)()(0, cart.show)()const show = cart.show; show()const price = cart.pricecart.price = 4
Put each line of code in its group: calls that keep the cart as this, calls that lose it, and plain reads or writes.
Common mix-ups
The word "reference" means two different things, which causes confusion. People often say a variable holds a "reference" to an object; that just means it points at the object. A spec Reference Record is the short-lived address from this lesson. It only says where to find something, and it disappears right after it is used.
const cart = { price: 3 };
console.log((0, cart.price));Line 1 makes a cart with price 3. Line 2 looks at 0, then at cart.price, and prints 3. The comma doesn't delete anything. It reads the value and hands it on, just without the address.
- "Brackets around a method cut it off from its object." No.
(cart.show)()keeps the address, so it still uses the cart. - "A method always remembers its object." No. The way you call it decides
this. A saved copy forgets. - "A call with no object always has
thisasundefined." Only in strict code. Older non-strict code usesglobalThis. - "
typeofnever throws." It is safe for missing names, but aletorconstused too early still throws. - "
supermakes the parentthis." No. It finds the method on the parent but keeps your object asthis.
The table below sums up the spec terms from this lesson. The last column lists common wrong guesses.
| Term | What it means | What it doesn't mean |
|---|---|---|
| Reference Record | A short-lived address that says where to find a name or property. | It isn't the object a variable points to. People call that a "reference" too, but it's a different thing. |
| GetValue | Use the address to read the value. | It doesn't change the object. And the value you get back doesn't remember where it came from. |
| GetThisValue | Ask the address which object should be this. | It doesn't always pick the parent. With super, this is still your own object. |
| A strict call with no object | The function runs, and this is undefined. | It doesn't throw an error by itself. Only reading something like this.price would throw. |
The next two examples are the same except for one line: "use strict". Both call a function with no object in front. Compare what this becomes.
function show() { return this === globalThis; }
const cart = { show };
const detached = cart.show;
console.log(detached());Line 1 makes a non-strict function that checks whether this is globalThis. Line 2 puts it on the cart. Line 3 saves the function. Line 4 calls it with no cart and prints true. When there's no object, older non-strict functions use the global object as this.
"use strict";
function show() { return this === undefined; }
const cart = { show };
const detached = cart.show;
console.log(detached());Line 1 turns on strict mode. Lines 2 and 3 make the method and put it on the cart. Line 4 saves the function. Line 5 calls it with no cart and prints true for this === undefined. Strict functions leave this as undefined.
Practice exercises
Guess what each short program does before you run it. The first ones ask for the output. The later ones ask you to fix a button callback and read a parent method. Try a guess first, then use the hints if you get stuck.
A cart has a method that reads its price. This time the call puts brackets around cart.show. What number does it print? Do the grouping brackets around cart.show change what this is?
const cart = {
price: 3,
show() { return this.price; },
};
const price = (cart.show)();
console.log(price);const price = (cart.show)(); // 3The grouping brackets around cart.show pass the address along, so this is the cart and show returns cart.price, which is 3.
This code checks a name that was never created. Write the two printed lines in order, separated by a comma. Why does only one of them throw?
console.log(typeof missingItem);
try {
console.log(missingItem);
} catch (error) {
console.log(error.name);
}// undefined
// ReferenceErrortypeof missingItem gives the text "undefined". Reading missingItem directly throws a ReferenceError, and the catch prints its name.
A discount changes the price in a cart. What number does the code print? Which line writes, and which line reads?
const cart = { price: 3 };
cart.price = 4;
console.log(cart.price);cart.price = 4;
console.log(cart.price); // 4Line 2 writes 4 into the cart. Line 3 reads the price again and gets 4.
A checkout button should show the cart's price. It was handed cart.show on its own, so when the button calls it, this is the button, not the cart. What should the callback call instead? Write the important expression.
"use strict";
const cart = { price: 3, show() { return this.price; } };
const button = { onclick: cart.show };
// Replace onclick with a callback that keeps the cart.
console.log(button.onclick());button.onclick = () => cart.show();
console.log(button.onclick()); // 3The arrow makes a fresh dot call through the cart each time it runs. So this is the cart, and it prints 3.
A child class asks its parent's method to read a name. The call passes in an object named Asha as this. Whose name does super.show() return, and why?
class Parent {
show() { return this.name; }
}
class Child extends Parent {
show() { return super.show(); }
}
console.log(new Child().show.call({ name: "Asha" }));console.log(new Child().show.call({ name: "Asha" })); // AshaThe parent's show runs, but this is still the object named Asha, so this.name is "Asha".
A strict function is saved from a cart and called by its new name. Does it print true, false, or throw? Then change the call to cart.show() and see what changes.
"use strict";
function show() { return this === undefined; }
const cart = { show };
const detached = cart.show;
console.log(detached());console.log(detached()); // trueCalled by its own name, the function gets no object. Strict code leaves this as undefined, so it prints true.
Check your understanding
For each question, ask one thing first: does the call still have the address (a dot call like cart.show()), or just a plain function? For reads and writes, ask whether the code takes a value out or puts one in. Each answer explains why it's right or wrong.
Question 1 of 7A Reference Record has four parts. Which part holds the name to look up, such as
price?Choose an answer to see the explanation.
Question 2 of 7What does this call in brackets print?
Read the code, then predictconst cart = { price: 3, show() { return this.price; }, }; const price = (cart.show)(); console.log(price);Choose an answer to see the explanation.
Question 3 of 7What does this comma call print?
Read the code, then predict"use strict"; const cart = { price: 3, show() { return this === undefined; }, }; console.log((0, cart.show)());Choose an answer to see the explanation.
Question 4 of 7What does this code print after the write?
Read the code, then predictconst cart = { price: 3 }; cart.price = 4; console.log(cart.price);Choose an answer to see the explanation.
Question 5 of 7What do these lines print for a name that was never created?
Read the code, then predictconsole.log(typeof missingItem); try { console.log(missingItem); } catch (error) { console.log(error.name); }Choose an answer to see the explanation.
Question 6 of 7What does this saved strict function print?
Read the code, then predict"use strict"; function show() { return this === undefined; } const cart = { show }; const detached = cart.show; console.log(detached());Choose an answer to see the explanation.
Question 7 of 7What does this
supercall print?Read the code, then predictclass Parent { show() { return this.name; } } class Child extends Parent { show() { return super.show(); } } console.log(new Child().show.call({ name: "Asha" }));Choose an answer to see the explanation.
Key takeaways
A method doesn't carry its object around. The way you call it decides whether JavaScript still knows which object to use as this.
- A Reference Record is a short-lived address. It says where to find a name or property. Its four parts are [[Base]], [[ReferencedName]], [[Strict]], and [[ThisValue]].
- GetValue uses the address to read. PutValue uses it to write.
- In
cart.show(), the address reaches the call, sothisis the cart. - Brackets around a method, like
(cart.show)(), keep the address. A comma, or saving the method in a variable, throws it away. - With no object, strict code leaves
thisasundefined; older non-strict code usesglobalThis. typeofis safe for missing names, andsuperfinds the method on the parent but keeps your object asthis.
Remember the one-liner.
Call a method with a dot, and it knows its object. Save it first, and it forgets.
Coming next: Environment Records: how scope is specified. It explains where plain variable names, like price, are found.