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.

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

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.

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:

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 2009, 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 *