Constructor in Javascript

In this article we are goanna to see about Constructor in Javascript and where we use these. which is most important when you are going to start your career in JavaScript. Please ready and enjoy this article if you want to give any feedback about your experience, we welcome.


Constructor is special function that prepare new instance of an object for use.

Example:

function Mobile( ){

this.model = “mi 9 pro”,

this.price = function( ){ document.write(this.model + “Price 3000”);

}

}

var redmi = new Mobile();

document.write(redmi.model + redmi.price( ));

Constructor with parameter:

Example:

function Mobile(model_name ){

this.model = model_name,

this.price = function( ){ document.write(this.model + “Price 3000”);

}

}

var redmi = new Mobile(“mi 9”);

document.write(redmi.model + redmi.price( ));


 Factory Function in Javascript

In this article we are goanna to see about Factory Function in Javascript and where we use these. which is most important when you are going to start your career in JavaScript. Please ready and enjoy this article if you want to give any feedback about your experience, we welcome.



A factory function is any function which is not a class or constructor that returns an object. In JavaScript, any function can return an object. When it does so without the new keyword, it’s a factory function.

Example:

function mobile( ){

return { 

model: “mi 9 pro”,

price: function( ){ return (“Price 3000”);}

};

}

var redmi = mobile( );

document.write(redmi.model + redmi.price( ));


Example:

function mobile(model_name ){

return { 

model : model_name;

price : function( ){ return (“Price 3000”);}

};

}

var redmi = mobile(“redmi pro 9” );

var nokia = mobile(“nokia 3000” );

document.write(redmi.model + redmi.price( ));


 Object Literal Properties and Method in Javascript

In this article we are goanna to see about Object Literal Properties and Method in Javascript and where we use these. which is most important when you are going to start your career in JavaScript. Please ready and enjoy this article if you want to give any feedback about your experience, we welcome.



Properties

A property of an object is some piece of named data it contains. These are accessed with dot operator applied to an object alternative to the dot operator is the array[ ] operator.

Syntax:  object_name.property_name

Ex:

var person = { fname: 'Raj’, lname: 'Kumar’, age: 25 };

var person = { };

person[“fname”] = “Raj”;   or person.fname =  “Raj”;

document.write(person.fname);

document.write(person[‘fname’]);

delete person.fname

Method

Object members that are functions are called methods. These are accessed with dot operator applied to an object alternative to the dot operator is the array[ ] operator.

Syntax:  object_name.method_name( );

Ex:

var person = { fname: 'Raj’, lname: 'Kumar’, fullname: function(){return (“Hello world”}}

var person = { };

person[“fullname”] = function(){return (“Hello world”)};  

person.fullname = function(){return (“Hello world”)}; 

document.write(person.fullname());

document.write(person[‘fullname’]());

       delete person.fname()


 Objects in Javascript

In this article we are goanna to see about Objects in Javascript and where we use these. which is most important when you are going to start your career in JavaScript. Please ready and enjoy this article if you want to give any feedback about your experience, we welcome.


An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects. 

Syntax:

objectName.propertyName

Ex:

var person = { fname: 'Raj’, lname: 'Kumar’, age: 25 };

document.write(person.fname);

document.write(person.lname);

Almost "everything" is an object javascript.

Booleans can be objects (if defined with the new keyword)

Numbers can be objects (if defined with the new keyword)

Strings can be objects (if defined with the new keyword)

Dates are always objects

Maths are always objects

Regular expressions are always objects

Arrays are always objects

Functions are always objects

Objects are always objects

All JavaScript values, except primitives, are objects

Declaration and initialization of Object:

Syntax:

var object_name = { };

Ex:

var person = { };

person[‘fname’] = “Rajesh”;

person[‘lname’] = “Kumar”;

person[‘age’] = 26

document.write(person.fname);



 OOPS Concept in Javascript

In this article we are goanna to see about OOPS concept in Javascript and where we use these. which is most important when you are going to start your career in JavaScript. Please ready and enjoy this article if you want to give any feedback about your experience, we welcome.



Object Oriented Programming Concept

Abstraction: 

Abstraction is a way of creating a simple model of a more complex real-world entities, which contains the only important properties from the perspective of the context of an application.

An abstraction is a way of hiding the implementation details and showing only the functionality to the users. In other words, it ignores the irrelevant details and shows only the required one.

Important point:

We cannot create an instance of Abstract Class.

It reduces the duplication of code.

2. Encapsulation: 

The Encapsulation is a process of binding the data (i.e. variables) 

with the functions acting on that data. It allows us to control the data and validate it. 

To achieve an encapsulation in JavaScript: -

Use var keyword to make data members private. Use setter methods to set the data and getter methods to get that data.

3. Inheritance: 

The inheritance is a mechanism that allows us to create new classes on the basis of already existing classes. It provides flexibility to the child class to reuse the methods and variables of a parent class.

The JavaScript extends keyword is used to create a child class on the basis of a parent class. It facilitates child class to acquire all the properties and behavior of its parent class.

4. Polymorphism: 

The polymorphism is a core concept of an object-oriented paradigm that provides a way to perform a single action in different forms. It provides an ability to call the same method on different JavaScript objects.

As JavaScript is not a type-safe language, we can pass any type of data members with the methods.


 OOPS in Javascript

In this article we are goanna to see about OOPS in Javascript and where we use these. which is most important when you are going to start your career in JavaScript. Please ready and enjoy this article if you want to give any feedback about your experience, we welcome.



Object-oriented programming(OOP) is a programming language model organized around objects rather than “actions” and data rather than logic. Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.

Object-oriented programming has several advantages over procedural programming:

1. OOP is faster and easier to execute

2. OOP provides a clear structure for the programs

3. OOP helps to keep the code DRY "Don't Repeat Yourself", and makes the code easier

     to maintain, modify and debug

4. OOP makes it possible to create full reusable applications with less code and shorter 

    development time

Classes and Objects

Classes and objects are the two main aspects of object-oriented programming.

Ex:

Class :- Fruits

Object:- Apple, Mango, Banana