JavaScript Fundamentals
JavaScript Objects
JavaScript Objects
Section titled “JavaScript Objects”Objects are collections of key-value pairs that store related data and functionality.
Creating Objects
Section titled “Creating Objects”Object Literal
Section titled “Object Literal”const person = { firstName: "John", lastName: "Doe", age: 30, fullName() { return `${this.firstName} ${this.lastName}`; }};Constructor Function
Section titled “Constructor Function”function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName;}
const john = new Person("John", "Doe");ES6 Classes
Section titled “ES6 Classes”class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; }
fullName() { return `${this.firstName} ${this.lastName}`; }}
const john = new Person("John", "Doe");Accessing Properties
Section titled “Accessing Properties”const person = { name: "Alice", age: 25 };
// Dot notationperson.name; // "Alice"
// Bracket notationperson["age"]; // 25
// Destructuringconst { name, age } = person;Object Methods
Section titled “Object Methods”const obj = { a: 1, b: 2, c: 3 };
Object.keys(obj); // ["a", "b", "c"]Object.values(obj); // [1, 2, 3]Object.entries(obj); // [["a", 1], ["b", 2], ["c", 3]]
// Spread operatorconst newObj = { ...obj, d: 4 };
// Object.assignconst merged = Object.assign({}, obj, { d: 4 });Prototypes
Section titled “Prototypes”function Animal(name) { this.name = name;}
Animal.prototype.speak = function() { console.log(`${this.name} makes a sound`);};
const dog = new Animal("Dog");dog.speak(); // "Dog makes a sound"