+ 2

In java script, their is no class but we do have constructors and we can create objects from that. How is it actually happening?

6th Jan 2017, 2:07 PM
Atul Raj
Atul Raj - avatar
3 odpowiedzi
+ 3
//contructor syntax function Classname() { // methods goes here this.exampleMethod = function() { // method code}; }// end of contructor //creating objects using constructor above var newObject = new Classname(); var anotherObject = new Classname();
6th Jan 2017, 2:19 PM
Igor Busquets
Igor Busquets - avatar
+ 2
i think u have different questions: where are the variables declared, those which are called by the constructor? when u use the "this. " inside the constructor and type a name, you are automatically declaring that as a new variable that belongs to that object. javascript does that, no need to use the var word. can we call the object thats called by the constructor? yeah! function Object() { this.propertyName = 10; this.methodName = function() {//method stuff}; } // creating objects and calling their properties var thing = new Object(); thing.Propertyname =5; thing.MethodName();
7th Jan 2017, 12:54 AM
Igor Busquets
Igor Busquets - avatar
0
True that we can use new keyword to call the constructor , so constructor is initializing the variables using this keyword, but there is no concept of class so where are these variables declared and can we call the object as it's an object of a class?
6th Jan 2017, 2:34 PM
Atul Raj
Atul Raj - avatar