Objects in JavaScript are collections of key-value pairs. Here's an example:
let person = {
name: "John",
age: 25,
isStudent: true
};
console.log(person.name); // Output: "John"
You can access object properties using dot notation or bracket notation. Example:
let person = {
name: "John",
age: 25,
isStudent: true
};
console.log(person.age); // Output: 25
console.log(person["isStudent"]); // Output: true
Objects can contain functions as properties, which are called object methods. Example:
let person = {
name: "John",
greet: function() {
console.log("Hello, " + this.name + "!");
}
};
person.greet(); // Output: "Hello, John!"
Object destructuring allows you to extract object properties into separate variables. Example:
let person = {
name: "John",
age: 25,
isStudent: true
};
let { name, age } = person;
console.log(name); // Output: "John"
console.log(age); // Output: 25
Classes are used to define object blueprints. Here's an example of creating a class and an object instance:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log("Hello, " + this.name + "!");
}
}
let john = new Person("John", 25);
john.greet(); // Output: "Hello, John!"
Constructor functions are used to create objects. Example:
function Person(name, age) {
this.name = name;
this.age = age;
this.greet = function() {
console.log("Hello, " + this.name + "!");
};
}
let john = new Person("John", 25);
john.greet(); // Output: "Hello, John!"
JavaScript uses prototypes for inheritance. Example:
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.greet = function() {
console.log("Hello, " + this.name + "!");
};
function Student(name, age, major) {
Person.call(this, name, age);
this.major = major;
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;
let john = new Student("John", 20, "Computer Science");
john.greet(); // Output: "Hello, John!"
ES6 introduced a simpler syntax for creating classes. Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log("Hello, " + this.name + "!");
}
}
class Student extends Person {
constructor(name, age, major) {
super(name, age);
this.major = major;
}
}
let john = new Student("John", 20, "Computer Science");
john.greet(); // Output: "Hello, John!"