The document explains the concepts of objects and classes in JavaScript. Objects are collections of key-value pairs that group related data and actions, while classes serve as blueprints for creating objects with defined properties and methods. Examples illustrate how to create objects and classes, including the use of constructors and methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views10 pages
JavaScript
The document explains the concepts of objects and classes in JavaScript. Objects are collections of key-value pairs that group related data and actions, while classes serve as blueprints for creating objects with defined properties and methods. Examples illustrate how to create objects and classes, including the use of constructors and methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10
JavaScript
Objects and Classes What Are Objects and Classes?
Objects: A collection of related data and
actions (properties and methods).
Classes: Blueprints for creating objects
with the same structure. What is an Object? An object is a collection of key-value pairs, where the keys are strings and the values can be of any data type and actions (methods).
It helps group data together logically.
Objects represent real-world items like a car, a user, or a product. Objects are created using {} (curly braces)
Data inside an object is stored as key-
value pairs. Example: const car = { brand: "Toyota", // Property color: "Red", // Property honk: function() { // Method console.log("Beep Beep!"); } }; console.log(car.brand); // Outputs: Toyota console.log(car["color"]); // Outputs: Red What is a Back to Agenda Page
Class? A class is a blueprint or template for
creating objects. It defines the properties (data) and methods (actions) that the objects will have.
Example: A "Car Factory" that makes
multiple cars with different brands and colors. Use the class keyword followed by the class name.
Inside the class, you define:
A constructor to initialize properties. Methods to define actions for the objects. Example: class Car { constructor(brand, color) { // The constructor method initializes properties this.brand = brand; // Set the brand property this.color = color; // Set the color property }
honk() { // Method to make the car honk
console.log("Beep Beep!"); } } Creating Object From a Class: const car1 = new Car("Toyota", "Red"); // Creates a car object const car2 = new Car("Honda", "Blue"); // Creates another car object Accessing Object From Class: console.log(car1.brand); // Outputs: Toyota console.log(car1.color); // Outputs: Red car1.honk(); // Outputs: Beep Beep! Thank you!