In JavaScript, we do not have any native approach to creating classes, but we can create a class using prototype inheritance and a constructor function.
Classes are containers for objects. We use classes to encapsulate a namespace and logic.
To instantiate a class, we can use the new
keyword. Classes are similar to constructor functions. Here is an example:
function student(nameI) { This.name=name; this.age='18'; } student.prototype.std=function() { //define some code }; module.export=student;
Classes and their modules are extremely important and vital aspects of JavaScript. We will be covering the following topics in the subsequent sections:
In JavaScript, a class is an object with special types of properties and methods. It is used to create instances and define the behavior of instances. Instances' behavior changes dynamically and we have a special syntax to write it in JavaScript. Instances are created when special methods are invoked on a class.
Prototypes and functions are two different things in JavaScript. A constructor can be any function, but a prototype is a special kind of object. To define a behavior of any instance, we use a prototype. There are no special properties or methods a prototype has. When we modify a prototype, we have instances.
In simpler words, we can say that in JavaScript, a constructor can be any function that is responsible for creating an instance. On the other hand, a prototype can be any object that has no special methods or properties. Prototypes are responsible for an instance's behavior.
In JavaScript, the prototype and constructor functions act like a class, because a class has a constructor, and to define methods, you have a prototype:
//Constructor Function var Cat = function(name) { this.Name = name; this.Meow = function() { console.log(this.Name + " meow"); }; } //Prototype object method var Cat = function(name) { this.Name = name; } Cat.prototype.Meow = function() { console.log(this.Name + " meow"); }; //Both gives same results var cat = new Cat("Capri"); cat.Meow();
There are no special properties or objects associated with a prototype. An object in your script can be an empty project or a prototype. When we say that any object can be a prototype, then this object will have functions and data types. Prototypes are not special kinds of objects, but classes are special kinds of objects in JavaScript.
A class contains a set of objects, and for initialization of the objects, we use constructors. In a class, we can create objects using the new
operator. We can define a class as a subclass to construct objects.
With a single property of prototype, a constructor is created. If we overwrite the prototype property, the reference to the constructor might be lost. In JavaScript, we use the constructor
property to create a class from an object that is passed as an object. Here is an example:
var color = 'black'; function Cat() { // public property this.color = ''; // private constructor var __construct = function(that) { console.log("I have a Cat!"); that.color = 'brown'; }(this) // getter this.getColor = function() { returnthis.color; } // setter this.setColor = function(color) { this.color = color; } } var b = new Cat(); console.log(b.getColor()); // should be brown b.setColor('white'); console.log(b.getColor()); // should be white console.log(color); // should be black
There are three ways of defining a class in JavaScript:
For specific objects' properties, the ECMA5 script added five methods. These methods are used to secure and restrict the extensibility of objects in the script. These methods are:
When we define a class in JavaScript, these methods are very useful. When you store an object, it sets an object ID for these methods. When we use a looping statement, it will return this object ID. All objects inherit the object ID, which is enumerable. To read a property, it will invoke the getter
function, and there will be no setter
function. So, it will be read-only. We cannot modify it, so it cannot be deleted.
The two very important aspects of modules are:
$function
, module that depends on this function will also have access to this function.A module can export a number of multiple variables and functions. To export these functions and variables, we use the export
keyword. Here is an example:
Export function sub(a,b) { Return a+b; }
We can also store a variable after exporting it. Here is an example:
Var string=function(a,b) { Return a+b; }
3.145.191.134