Objects and Prototypal Inheritance
About 430 wordsAbout 5 min
2025-08-05
This section uncovers JavaScript's unique object model, from property descriptors to the prototype chain that underpins the class syntax.
- Object Property Descriptors Every property on an object is defined by more than just its value. It has a "property descriptor" with internal attributes:
value: The value of the property.writable: Iftrue, the value can be changed.enumerable: Iftrue, the property will appear infor...inloops andObject.keys().configurable: Iftrue, the property can be deleted, and its attributes (exceptvalueandwritableifwritableisfalse) can be changed.
You can view and modify these with Object.getOwnPropertyDescriptor() and Object.defineProperty(). This allows for fine-grained control over object behavior.
const obj = {};
Object.defineProperty(obj, 'readOnly', {
value: 42,
writable: false,
enumerable: true,
configurable: false
});
// obj.readOnly = 100; // Fails silently (or throws in strict mode)- The Prototype Chain Explained Inheritance in JavaScript works via prototypes.
`prototype` Property
Every function has a special prototype property. This is an object that will become the prototype for all instances created by that function when used as a constructor with new.
`[[Prototype]]` (or `__proto__`)
Every object instance has a hidden [[Prototype]] link (often accessible via __proto__) that points to its prototype object.
When you access instance.someMethod(), the engine first looks for someMethod on instance. If not found, it follows the [[Prototype]] link and looks on instance's prototype. This continues up the chain until the method is found or the end of the chain (Object.prototype, which has a null prototype) is reached.
Object.create(): This is a direct way to create a new object with a specified prototype, bypassing the need for a constructor function. const child = Object.create(parent);
- ES6 Classes: Syntactic Sugar and Advanced Features The
classsyntax provides a cleaner way to work with prototypes.
extends and super():
extendssets up the prototype chain.class Child extends Parent {}linksChild.prototypetoParent.prototype.super()must be called in the child class'sconstructorbefore usingthis. It calls the parent constructor to initialize the parent part of the object.super.method()can be used to call a method from the parent class.
Private Fields (#): Modern JavaScript allows for truly private class fields using a # prefix. These fields are inaccessible from outside the class instance.
class Counter {
#count = 0; // Truly private field
increment() {
this.#count++;
}
getValue() {
return this.#count;
}
}
const c = new Counter();
// console.log(c.#count); // SyntaxError: Private field must be declared in an enclosing classChangelog
27885-feat: add new notes on Modern ES6+ Features, Objects and Prototypal Inheritance, and Functionson
Copyright
Copyright Ownership:WARREN Y.F. LONG
License under:Attribution-NonCommercial-NoDerivatives 4.0 International (CC-BY-NC-ND-4.0)