objects

A state object I wrote once had a salary field that was never meant to be public in found it That was the day I started thinking about property ownership explicitly

Shape and destructuring

const person = { first: "Ada", last: "Lovelace" };
const { first } = person;

first is a new binding Changing it does not change person.first

Shorthand

const activity = "code";
const day = { activity };

Same as { activity: activity } Useful when a variable name already describes the key

Getters and setters

const dog = {
  _name: "Pip",
  get name() { return this._name; },
  set name(value) { this._name = value; }
};

dog.name = "Rex";
console.log(dog.name);

Output:

Rex

this refers to the object the method was called on Arrow functions do not get their own this , so do not use them for object methods that need it

Ownership

"salary" in person

Output:

false

in walks the prototype chain Use Object.hasOwn(person, "salary") when you need the property to be directly on the object

Next

Blueprints in 08 classes

Posted