Lecture 11 - Classes & Objects
Lecture 11 - Classes & Objects
Objects in JavaScript
What is a JavaScript object?
A JavaScript object is an entity with:
o State: These are the object's properties (e.g., a car's color or speed).
o Behavior: These are the object's methods (e.g., a car can drive).
Why is it called an Entity?
An entity has:
o State: The properties that hold data.
o Behavior: The methods that define actions the object can do.
Example:
o A car is an entity (JavaScript object):
State: Properties like brand, model, and year.
Behavior: A method like startEngine().
Summary:
An entity in JavaScript is simply an object with both properties (state) and methods
(behavior), which allows it to store information and perform actions.
2. Prototypes in JavaScript
Definition: Every object in JavaScript has a prototype.
o Purpose: A prototype is another object that shares properties and methods.
o How It Works:
If an object doesn't have a property or method, JavaScript looks at its
prototype to find it
This process continues up the prototype chain until it finds the property or
method, or until it runs out of prototypes.
4. Classes in JavaScript
Definition: A class is like a blueprint to create objects.
o Purpose: It defines what an object should have (properties) and what it should do
(methods).
How It Works:
o We can create many objects from one class, each with their own values but the
same behavior.
Summary:
Object: A specific thing with properties and methods (e.g., a car).
Prototype: A special object that allows sharing properties and methods.
Class: A blueprint for creating multiple similar objects.
3. What is a Prototype?
A prototype is a "toolbox" that an object can use to borrow features (like methods or
values) from another object.
Conclusion
Class: A template for creating objects.
Object: The actual item created using the template (class).
Prototype: A shared toolbox that allows objects to inherit properties and methods
from other objects, saving memory and making code cleaner.
1. Objects in JavaScript
What are Objects?
o Objects are collections of properties (key-value pairs).
o They can store data (like variables) and functions (called methods).
How to Create an Object
const car = {
brand: "Toyota",
model: "Corolla",
start: function() {
console.log("Car is starting...");
}
};
o Here, car is an object with properties (brand, model) and a method (start()).
2. Prototypes in JavaScript
What are Prototypes?
o Every JavaScript object has a hidden property called __proto__, which points to
another object called its prototype.
o Prototypes allow objects to inherit properties and methods from other objects.
How Prototypes Work
const animal = {
sound: "Roar",
makeSound: function() {
console.log(this.sound);
}
};
const lion = {};
lion.__proto__ = animal; // lion inherits from animal
lion.makeSound(); // Output: Roar
o lion doesn't have its own makeSound method, so it looks up to its prototype
(animal) for that method.
3. Classes in JavaScript
What are Classes?
o Classes are a way to create objects with a blueprint.
o They are like templates for creating multiple objects with similar properties and
methods.
o Introduced in ES6 (2015), they make JavaScript more structured.
How to Create a Class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name}.`);
}
}
const john = new Person("John", 30);
john.greet(); // Output: Hello, my name is John.
o Person is a class. We use new to create objects (instances) from this class.
o The greet method is shared by all instances of Person via the prototype.
Conclusion
Objects are fundamental building blocks.
Prototypes provide a way for objects to share behavior and properties.
Classes offer a cleaner, more structured way to create objects, especially when you
need multiple similar objects.
This hierarchy makes JavaScript flexible and powerful, allowing developers to use different
patterns for creating and managing objects.