In JavaScript, when you create any object through a constructor call, as shown below
Step 1: create a say Person function ..
function Person(name){ this.name=name; } person.prototype.print=function(){ console.log(this.name); }
Step 2: create an instance for this function.
var obj=new Person('venkat')
// this function (Person) will be created above the line and return a new Person object (name: 'venkat'}
if you do not want to instantiate this function and call at the same time. We can also do as below.
var Person = { init: function(name){ this.name=name; }, print: function(){ console.log(this.name); } }; var obj=Object.create(Person); obj.init('venkat'); obj.print();
in the above method, init will help in creating the properties of the object. basically init is like calling the constructor of your class.
Venkat Jan 14 '16 at 4:08 2016-01-14 04:08
source share