Working with Objects
const book = { title: "JavaScript: The Good Parts", author: "Douglas Crockford", yearPublished: 2008, pages: 176, isInPrint: true};There are several ways to create objects in JavaScript:
The most common way to create objects is using object literal notation:
const person = { firstName: "John", lastName: "Doe", age: 30};Objects can contain other objects as properties, creating a nested structure.
let person = { name: 'Alice', address: { street: '123 Main St', city: 'Wonderland' }};
console.log(person.address.city); // Output: WonderlandES6 (2015) introduced class syntax, which provides a more familiar way to create objects for programmers coming from class-based languages:
class Person { constructor(firstName, lastName, age) { this.firstName = firstName; this.lastName = lastName; this.age = age; }
getFullName() { return `${this.firstName} ${this.lastName}`; }}
const john = new Person("John", "Doe", 30);console.log(john.getFullName()); // "John Doe"Constructor functions let you create multiple objects with the same structure:
function Person(firstName, lastName, age) { this.firstName = firstName; this.lastName = lastName; this.age = age;}
const john = new Person("John", "Doe", 30);const jane = new Person("Jane", "Smith", 25);You can access object properties in two ways:
- Syntax:
objectName.propertyName
const person = { name: "John", age: 30 };console.log(person.name); // "John"person.age = 31;- Syntax:
object["property"]
const propertyName = "name";console.log(person[propertyName]); // "John"
// Adding a property with spaces in its nameperson["favorite color"] = "blue";ES6 introduced destructuring to extract properties into variables.
const user = { name: "Eve", age: 28 };const { name, age } = user;console.log(name, age); // "Eve", 28
// With aliasingconst { name: userName } = user;console.log(userName); // "Eve"Properties can be added or updated after creation.
const person = { name: "Charlie" };person.age = 30; // Addperson.name = "Charlotte"; // ModifyUse the delete operator to remove a property.
delete person.age;console.log(person.age); // undefinedTo check if a property exists in an object, you can use:
console.log("make" in car); // trueconsole.log("model" in car); // false after deletionconsole.log(car.hasOwnProperty("make")); // trueUnlike the in operator, hasOwnProperty() only checks for the object’s own properties, not properties inherited from its prototype.
console.log(car.model === undefined); // true if the property doesn't existMethods are functions stored as object properties. They allow objects to have behavior in addition to data.
const dog = { name: "Rex", bark() { console.log(`${this.name} says woof!`); }};
dog.bark(); // "Rex says woof!"- Shorthand Method Syntax: ES6 allows omitting the
functionkeyword.
Use extends for inheritance in ES6 classes.
class Vehicle { constructor(type) { this.type = type; } move() { console.log(`${this.type} is moving`); }}
class Car extends Vehicle { constructor(make, model) { super("Car"); // Call parent constructor this.make = make; this.model = model; }}
const myCar = new Car("Hyundai", "Sonata");myCar.move(); // "Car is moving"