I have following piece of code. In this code, I have a class called func
. Inside func
, I have 3 functions i.e. init, func1 and func2
. Then I am calling init function before return object literal
. So what happens is when I create new instance of this class using var f = new func()
then it automatically calls the init
function so I don't have call it manually. So its like constructor in other object oriented languages when a new instance of a class is created then constructor automatically gets called.
Here is the code
var func = function () {
var a, b,
init = function () {
a = 1;
b = 2;
},
func1 = function () {
},
func2 = function () {
};
init();
return {
func1: func1,
func2: func2
};
};
var f = new func();
Now I want to do the exact same thing as above in following scenario. That is I want the init
function get automatically called whenever I call any function inside func
object.
var func = {
init: function () {
},
func1: function () {
},
func2: function () {
}
}
func.func1();
Is it possible to make init function automatically called whenever I call any function inside func object in above scenario? If yes then How?