Course contentsShow
← JavaScript
Lesson 701 of 2,72317. Prototypes and InheritanceFree lesson

Prototypes vs Own Properties

Distinguishing between properties defined directly on an object and those inherited via prototype.

Prototypes vs Own Properties

You'll learn how to distinguish between properties that live directly on an object and those it inherits from its prototype.

The Core Difference

When you access a property on an object, JavaScript checks two places: first the object itself (called own properties), then its prototype chain (called inherited properties). Understanding this distinction is crucial for debugging and working with objects effectively.

Think of it like your personal belongings versus your family's shared items. Your phone is yours (own property), but the TV in the living room belongs to the household (inherited through the family prototype).

Checking Own vs Inherited Properties

const parent = { familyName: 'Smith' };
const child = Object.create(parent);
child.firstName = 'Alex';

console.log(child.firstName);    // 'Alex' (own property)
console.log(child.familyName);   // 'Smith' (inherited from prototype)

// Check if it's an own property
console.log(child.hasOwnProperty('firstName'));   // true
console.log(child.hasOwnProperty('familyName'));  // false

// Both work with 'in' operator
console.log('firstName' in child);   // true
console.log('familyName' in child);  // true

Why This Matters

Own properties override inherited ones. If child defines its own familyName, it "shadows" the prototype's version:

child.familyName = 'Johnson';
console.log(child.familyName);  // 'Johnson' (own property now)
console.log(parent.familyName); // 'Smith' (prototype unchanged)

Use hasOwnProperty() or Object.hasOwn() when you specifically need to check if a property belongs directly to the object, not its prototype.

Key Takeaway: Own properties exist directly on an object; inherited properties come from the prototype chain. Use hasOwnProperty() to distinguish between them.