Classes and Constructors
Classes and Constructors
Constructor
You can create objects in JavaScript using curly braces { … } syntax. But what if you need
to create multiple similar objects. You can either write the same syntax for every object,
or you can use the constructor to create objects.
Using { … } syntax to create multiple objects can create certain inconsistencies in code-
Objects can be created by calling the constructor function with the new keyword.
Using a constructor means that -
● All of these objects will be created with the same basic structure.
● We are less likely to accidentally make a mistake than if we were creating a whole
bunch of generic objects by hand.
It is important always to use the new keyword when invoking the constructor.
If new is not used, the constructor may clobber the 'this', which was accidentally passed
in most cases as the global object (window in the browser or global in Node.). Without
the new function will not work as a constructor.
1
this.age = age;
}
The new keyword is the one that is converting the function call into constructor
call, and the following things happen -
NOTE: this referred in the constructor bound to the new object being constructed.
Classes
Classes are introduced in ECMAScript 2015. These are special functions that allow
one to define prototype-based classes with a clean, nice-looking syntax. It also
introduces great new features which are useful for object-oriented programming.
The way we have defined the class above is known as the class declaration in order to
create a new instance of class Person by using the new keyword.
2
● The constructor method is a special method for creating and initialising an
object.
● You cannot use the constructor method more than once; else, SyntaxError is
thrown.
● Just like the constructor function, a new keyword is required to create a new
object.
typeOf(Person) ; // function
class Vehicle {
constructor(variant) {
this.model = variant;
}
get model( ) {
return this.model;
}
set model(value) {
this.model = value;
}
}
3
Class Expression
A class expression is another way to define a class, which is similar to a function
expression. They can be named and unnamed both, like -
NOTE: The name given to the class expression is local to the class’s body.
Hoisting
Class declarations are not hoisted. If you try to use hoisting, it will return not defined
error.
Instance
It is made using the blueprint of the class having its behaviours and properties.
Example: Let’s say Human is a class, and we are all its instances
class Human{
constructor(name , height , weight){
this.name = name;
this.height=height;
this.weight=weight;
}
}
Encapsulation
The process of wrapping property and function within a single unit is known as
encapsulation.
4
class Person{
constructor(name,age){
this.name = name;
this.age = age;
}
add_Address(add){
this.add = add;
}
getDetails(){
console.log("Name is",this.name,"Address is:", this.add);
}
}
In the above example, we simply created a person instance using the constructor and
Initialized its properties.
Encapsulation refers to the hiding of data or data Abstraction which means
representing essential features hiding the background detail. Most of the OOP
languages provide access modifiers to restrict the scope of a variable, but there are no
such access modifiers in JavaScript but there are certain ways by which we can restrict
the scope of variable within the Class/Object.
function person(firstname,lastname){
let firstname = firstname ;
let lastname = lastname ;
let getDetails_noaccess = function( ){
return (“Name is:”, this.firstname , this.lastname) ;
}
this.getDetails_access = function( ){
return (“Name is:”, this.firstname , this.lastname) ;
}
}
5
In the above example we try to access some property person1.firstname and functions
person1.getDetails_noaccess but it returns undefined while their is a method which
we can access from the person object person1.getDetails_access( ) and by changing
the way to define a function we can restrict its scope.