Class constructor and static in JavaScript
Getting started from the basics of constructors, Instance methods and Static Methods.
Constructors
Purpose: The
constructor
is a special method that gets called when you create an instance of a class. It's used to initialize the object's properties.
Example:
class Person {
constructor(name, age) {
this.name
= name;
this.age = age;
}
}
const Amir = new Person("Amir", 30); //
console.log(Amir.name);
// Outputs: "Amir
"
Note: In object-oriented programming (OOP), a class is like a blueprint or a template for creating objects.
class as a blueprint:
- Just as a blueprint for a building provides instructions on how to construct a building, a class provides instructions on how to create objects with specific properties (attributes) and behaviors (methods).
Properties and Methods:
Within a class, you define the properties (attributes) that objects created from the class will have. These properties define the state of the objects.
You also define methods (functions) within the class that specify the behaviors or actions that objects can perform.
Objects as Instances:
When you create an object from a class, you are essentially following the instructions in the blueprint to construct an instance of that class.
Each object created from the same class will have the same properties and methods defined in the blueprint, but they can have different values for their properties.
Example:
Imagine a
Car
class as a blueprint. It defines properties likemake
,model
, andyear
(e.g., "Toyota," "Camry," "2023") and methods likestartEngine
andstopEngine
.When you create a
Car
object, it becomes an instance of theCar
class, with its own specific make, model, and year values.You can then call methods on the
Car
object to start or stop its engine.
Instance Methods:
Purpose: Instance methods are functions defined within a class that can be called on instances of the class. They can access and operate on instance-specific data. Instance methods are called on instances of the class and can access the instance's properties through the this
keyword.
Example:
class Person {
constructor(name, age) {
this.name
= name;
this.age = age; }
sayHello() {
console.log(Hello, my name is ${
this.name
} and I am ${this.age}
years old.);
}
}
const Amir = new Person("Amir", 30);
Amir.sayHello();
// Outputs: "Hello, my name is Amir and I am 30 years old."
"Here, sayHello
is an instance method that can be called on Amir[instance of class]
, and it accesses Amir
's properties."
Static Methods:
Purpose: Static methods are methods that belong to the class itself, rather than instances of the class. They are often used for utility functions related to the class but not dependent on instance-specific data.
Example:
class MathUtility {
static square(x) {
return x * x;
}
}
const result = MathUtility.square(5);
console.log(result);
// Outputs: 25
"In this example, square
is a static method of the MathUtility
class. You can call it without creating an instance of the class."
To summarize, the constructor
initializes instance-specific properties when creating objects from a class. Instance methods
operate on those properties and are called on instances. Static methods
belong to the class itself and are called on the class, often for utility functions that don't depend on instance data.
#programming #JavaScript #webdevelopement #basicsofprogramming #oops #class #constructors