Read, change, add and delete properties, give an object a method, and see two names share one object.
JavaScript · beginner · 6 min · 10 steps · free
const book = {
title: 'Dune',
pages: 412,
describe() {
return `${this.title}, ${this.pages} pages`;
},
};
book.pages = 420;
book.year = 1965;
const alias = book;
alias.title = 'Dune Messiah';
delete book.year;
console.log(book.describe());
console.log('pages' in book, Object.keys(book));book points to a new object with two properties and a method.pages in the Object that book points to changes from 412 to 420.year with the value 1965 is added to the Object that book points to.alias = book shares the same object, like with arrays.title in the Object that book and alias point to changes from "Dune" to "Dune Messiah".year is removed from the Object that book and alias point to.this set to book.describe returns "Dune Messiah, 420 pages".describe() has finished, so its variables are gone. It prints Dune Messiah, 420 pages."pages" in book checks for a key; Object.keys lists them.Answer: undefined
Answer: ["x", "y"]
← map, filter, reduce and find · Destructuring and Spread →