Front-end Programming Using Object-Oriented programming in javascript #3

The class

JavaScript is a prototype-based language and contains no class statement, like is found in C++ or Java. this is often typically confusing for programmers familiar with languages with a class statement. Instead, JavaScript uses functions as constructors for classes. defining a class is as simple as defining a function. within the example below we have a tendency to define a new category called Person with an empty constructor.

var Person = function () {};

The object (class instance)

To create a new instance of an object obj we tend to use the statement new obj, assigning the result (which is of type obj) to a variable to access it later. In the example on top of we tend to define a class named Person. within the example below we tend to create two instances (person1 and person2).

var person1 = new Person();
var person2 = new Person();

The constructor

The constructor is called at the moment of instantiation (the moment when the object instance is created). The constructor is a method of the class. In JavaScript the function serves as the constructor of the object, so there’s no ought to expressly define a constructor method. each action declared within the class gets executed at the time of instantiation.

The constructor is used to set the object’s properties or to call methods to prepare the object for use. Adding class methods and their definitions occurs using a different syntax represented later during this article.

In the example below, the constructor of the class Person logs a message once a person is instantiated.

var Person = function () {
  console.log('instance created');

var person1 = new Person();
var person2 = new Person();

The property (object attribute)

Properties are variables contained within the class; each instance of the object has those properties. Properties are set within the constructor (function) of the class so that they’re created on every instance.

The keyword this, that refers to the current object, permits you to work with properties from within the class. Accessing (reading or writing) a property outside of the class is done with the syntax: InstanceName.Property, similar to in C++, Java, and several other} other languages. (Inside the class the syntax this.Property is used to get or set the property’s value.)

In the example below, we tend to define the firstName property for the Person class at instantiation:

var Person = function (firstName) {
  this.firstName = firstName;
  console.log('Person instantiated');

var person1 = new Person('Alice');
var person2 = new Person('Bob');

// Show the firstName properties of the objects
console.log('person1 is ' + person1.firstName); // logs "person1 is Alice"
console.log('person2 is ' + person2.firstName); // logs "person2 is Bob"

I hope you will find this post very useful regarding Object Oriented Programming in Javascript. Let me know if you have any question regarding Javascript in comment . I will reply to you ASAP.

Have you got cool idea about Web Application Development ? Contact us Now to get free consultation on your idea. Alphanso Tech is rated as one of the best Web Application Development Company in India.

Post by Alphanso Tech

Created in 2013, Alphanso Tech is a leading IT Consulting company with a presence in the USA, Canada, and India.We are a trusted and leading service provider to many top-tier companies in the area of Custom WordPress Development , Web Application Development , iOS App Development , Outsourcing Services, Online Marketing and many other related activities.

Leave a Reply

Your email address will not be published. Required fields are marked *