Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  163] [ 3]  / answers: 1 / hits: 39698  / 13 Years ago, tue, february 14, 2012, 12:00:00

I was looking for a constructor or a init function for following situation:



var Abc = function(aProperty,bProperty){
this.aProperty = aProperty;
this.bProperty = bProperty;
};
Abc.prototype.init = function(){
// Perform some operation
};

//Creating a new Abc object using Constructor.

var currentAbc = new Abc(obj,obj);

//currently I write this statement:
currentAbc.init();


Is there a way to call init function when new object is initialized?


More From » oop

 Answers
4

You can just call init() from the constructor function



var Abc = function(aProperty,bProperty){
this.aProperty = aProperty;
this.bProperty = bProperty;
this.init();
};


Here is a fiddle demonstrating: http://jsfiddle.net/CHvFk/


[#87489] Sunday, February 12, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dawnc

Total Points: 612
Total Questions: 94
Total Answers: 98

Location: Sweden
Member since Fri, Apr 16, 2021
3 Years ago
;