JS: 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

JS Object Property

  1. Property Overview
  2. Property Key
  3. Dot vs Bracket Notation
  4. Create/Delete Property
  5. Get/Set Property
  6. Property Existence
  7. Access Property
  8. List Properties
  9. Property Attributes
  10. Getter/Setter
  11. Property Descriptor
  12. Symbol
Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials