JavaScript: Get Property, Set Property

By Xah Lee. Date: . Last updated: .

Reading Property Goes Up the Prototype Chain

When a property of a object is looked up (for example, obj.color), JavaScript first look at the object to see if it has that property, if not, it lookup its parent, and repeat, until a property is found or no more parent.

const xx = {"kk":3};

// create object yy, with xx as parent
const yy = Object.create(xx);

console.log( yy.kk === 3 ); // true
// key is from parent

// yy has no keys
console.log( Reflect.ownKeys ( yy ). length === 0 ); // true

Accessing Non-Existent Property Returns β€œundefined”

console.log( {a:2}.b === undefined); // true

Setting Property Never Touches Prototype Chain

Setting property never touches prototype chain.

When setting a value to a key, if the object has the property, its value is modified. If the object does not have the property, its created.

// setting a property will not touch prototype chain.

const xx = {"kk":3};

// create object yy, with xx as parent
const yy = Object.create(xx);

yy.kk = 4;

console.log( xx.kk === 3 ); // true

JavaScript Property

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc