let book = {
title: "Outliers",
published: 2011,
keywords: ["success", "high-achievers"]
};
let book = {
title: "Outliers",
published: 2011,
keywords: ["success", "high-achievers"],
author: {
firstName: "Malcolm",
lastName: "Gladwell"
}
};
book.isbn = "12345";
book.author.dob = "1963-11-03";
let book = {
title: "Quiet",
author: {
firstName: "Susan",
lastName: "Cain"
},
// Define a method
getAuthorName: function() {
return this.author.firstName + " " + this.author.lastName;
}
};
// Call method that returns "Susan Cain"
let name = book.getAuthorName();
book.showInfo = function() {
console.log(this.title + " by " + this.author.firstName + " " + this.author.lastName);
}
// Outputs "Quiet by Susan Cain"
book.showInfo();
get
keyword that is called when an object's property is retrieved
set
keyword that is called when an object's property is set to a value
area
let rectangle = {
width: 5,
height: 8,
get area() {
return this.width * this.height;
},
set area(value) {
// Set width and height to the square root of the value
this.width = Math.sqrt(value);
this.height = this.width;
}
};
let area = rectangle.area; // Calling getter returns 40
rectangle.area = 100; // Calling setter sets width and height to 10
console.log(rectangle.width); // 10
let x = 2;
let y = x; // y is a copy of x
let a = { xyz: 123 };
let b = a; // b is a copy of a's reference (both refer to same object)
function changeName(person) {
person.name = "Sue";
}
let bob = { name: "Bob" };
changeName(bob);
console.log(bob.name); // Sue
let bob = { name: "Bob" };
bob = null; // Garbage collector will eventually deallocate { name: "Bob" }