Understand Prototype Lookup
Understand Prototype Lookup
When an object lacks an own property, lookup can continue through its prototype. A shared school rule can be inherited, while an own property can override the visible value without changing the prototype.
Before this topic, review Choose an Array Operation if its terms are unfamiliar.
What you’ll learn
- Explain understand prototype lookup through the school example.
- Trace the shown operations and distinguish their result from displayed output.
- Make the practice change and explain its consequence: The student now owns the threshold, so lookup stops before the prototype.
Work through the school example
This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.
JavaScript · school.js · complete example
const rules = { passMark: 50 };
const student = Object.create(rules);
student.name = "Diya";
console.log(student.passMark);
console.log(Object.hasOwn(student, "passMark"));
Expected output
50
falseRun in JavaScript compiler →Recognize the important boundary
Lookup and ownership are different. Avoid treating arbitrary untrusted keys as harmless object paths; shared prototype changes can affect many objects.
Try a small variation
JavaScript · original source for practice
const rules = { passMark: 50 };
const student = Object.create(rules);
student.name = "Diya";
console.log(student.passMark);
console.log(Object.hasOwn(student, "passMark"));
Your task: Replace student.name = 'Diya' with student.passMark = 60. Predict the result before running the edited program.
The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.
Worked answer and reasoning
The student now owns the threshold, so lookup stops before the prototype.
JavaScript · complete worked variation
const rules = { passMark: 50 };
const student = Object.create(rules);
student.passMark = 60;
console.log(student.passMark);
console.log(Object.hasOwn(student, "passMark"));
Expected output
60
trueRun in JavaScript compiler →Predict the original program
JavaScript · original program for this question
const rules = { passMark: 50 };
const student = Object.create(rules);
student.name = "Diya";
console.log(student.passMark);
console.log(Object.hasOwn(student, "passMark"));
Check the important distinction
Keep the useful idea
When an object lacks an own property, lookup can continue through its prototype. A shared school rule can be inherited, while an own property can override the visible value without changing the prototype.
Check your variation against this explanation: The student now owns the threshold, so lookup stops before the prototype.
Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.
Reaching the recap and completing the quizzes are separate milestones.