Does javascript has Classes?

Working on this pretty language, One of my friend asked “Does javascript has classes?”,  I revert to him NO. As I give example how it is possible.  its confuse us and are contradictory statements.

So this article gives you a roadmap how OOPS concept implements in javascript.
JavaScript follow object oriented techniques and when we come to syntax javascript does not contain class statement as other oops based language does. So in javascript we use functions as classes. In the example below we have a function student.

function student(name)
{
console.log("class is Instantiated");
this.name = name;
}
var objStu = new student('abc');  // creating an instance (Constructor)
console.log(objStu.name);   //
 

This function serves as class in javascript and we can implement features of oops, like in example objStu is an object of class student and to get student name we can get by objStu.name.
Implementing Inheritance: Javascript use single class inheritance. Now we have a function sports which inherit parent class student
function sports() {}
We create a parent class object and assign to the child class
var sports = new student();
this way we can inherit parent class like we have a function setName that set all sports students

sports.setName = function(name) {
this.name = name;
alert("Sports student Name is"+ name);
}
sports.setName('xyz');

Now we have a clear understanding how we can implement oops in javascript and are very clear that all the base is javascript functions and its objects. Everything in javascript is an object either it can be an array, string or function.
Another Concept to define function, object and array in javascript is JSON (Javascript Object Notation). It is a light weighted data exchange format and all roll in json about array and object. (object and array are the main stream of json)
To create an object (A collection of compound statements):
var obj = {};
instead of
var obj = new Object();
likewise to create an array:
var myarr = [];
instead of:
var myarr = new Array();
let us take an example
Having variable obj having property color red and it name abc so we have an valiable obj

var obj = {
color: "red",
name: "abc",
}

This example illustrate that object holds a key/value pair. Like in example object obj hold a property name color, which is it key and  its value is red. now to get value of obj color and name we have to just  call obj property by dot(.) operator like:
console.log(obj.color); or console.log(obj.name);
extending function to above example:

var obj = {
color: "red",
name: "abc",
getItem: function () {
return "object name is:"+this.name + ' and color is: ' + this.name ;
}
}
console.log(obj.getItem());

in above example, an another way to define mehtod “getItem” declaration and if we want to call this function we did obj.getItem().
There is lot more to function, object in oops and javascript…hope this article helped you to explore javascript.

Leave a Reply

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

Time limit is exhausted. Please reload the CAPTCHA.



nike tn pas cher tn pas cher peuterey outlet piumini peuterey outlet spaccio peuterey outlet peuterey outlet piumini peuterey outlet spaccio peuterey outlet hogan outlet scarpe hogan outlet hogan outlet online peuterey outlet piumini peuterey outlet spaccio peuterey outlet hogan outlet scarpe hogan outlet hogan outlet online hogan outlet scarpe hogan outlet hogan outlet online doudoune moncler pas cher moncler pas cher doudoune moncler hogan outlet scarpe hogan outlet hogan outlet online nike tn pas cher tn pas cher