Classes and modules

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;

Note

Modules are used to include and extend classes and properties easily. Modules attach properties to global objects to export module values.

Classes and their modules are extremely important and vital aspects of JavaScript. We will be covering the following topics in the subsequent sections:

  • Classes and prototypes
  • Constructors
  • Java-style classes in JavaScript
  • Augmented JavaScript
  • Types of classes
  • Subclasses
  • Classes in ECMA5 script
  • Modules

Classes and prototypes

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:

Here is an example:

//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.

Constructors

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

Defining a class

There are three ways of defining a class in JavaScript:

  • Using a function
  • Using object literals
  • A singleton using a function

Using a function

You can create an object using the new keyword in a function. To access methods and properties, use the this keyword. There would be a conflict if you define a function with the same name in a class.

Using object literals

To define an object or array in JavaScript, we use literals. Here is an example:

Varobj= {};
Varobj=[];

Classes in the ECMA5 script

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:

  • Enumerable
  • Writable
  • Getter
  • Setter
  • Configurable

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.

Modules

The two very important aspects of modules are:

  • They have dependencies: This means that when you write a module in your system, it is fully dependent on the function. We import dependencies from functions while creating our application.
  • They have exports: If you leave some function and variable public in your system, anything can export these. For example, you have exported a function $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;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.191.134