Object Constructor | SoloLearn: Learn to code for FREE!


Object Constructor

function Person(name,age){ this.name = name; // what this doing person,devil what this pointing can't understand; this.age = age; } var devil = new Person("Devil",2000); // what new person doing; //is new Person doing this // Person = {} // when i type new person what's going on function Person; // after new person what function Person doing;

7/4/2018 3:04:48 PM

chander Bisht

4 Answers

New Answer


Sample code with classes: https://code.sololearn.com/WV6KLUxw18B6/?ref=app


As i know, and i hope i'm not wrong function Person(name,age){ this.age=age; this.name=name; /*here you created a property(variable of a class) name and assigned it to the name given to the function paramaters*/ } In your case here, Person is a class (you can also create a class this way"class Person(){}"). name and age are properties. So whenever you call this function with the "new" keyword on a variable like you did with devil, you call it's constructor wich is the instructions inside your function then an object is created and it's devil, now devil have a name and an age. When you use "this" it means you refer to the object itself, so when you do this "this.name=name" you're changing the name of the object with the new name.


understand but not fully


java class constructor function constructor are same