javascript - Can I overload an object with a function? -


let's have object of functions/values. i'm interested in overloading based on calling behavior.

for example, block of code below demonstrates wish do.

var main_thing = {     initalized: false,     something: "hallo, welt!",     something_else: [123,456,789],     load: {         sub1    : function() {             //some stuff         },         sub2    : function() {             //some stuff         },             : function() {             this.sub1();             this.sub2();         }     }     init: function () {         this.initalized=true;         this.something="hello, world!";         this.something_else = [0,0,0];         this.load(); //i want call this.load.all() instead.     } } 

the issue me main_thing.load assigned object, , call main_thing.load.all() call function inside of object (the () operator). can set code use main_thing.load access object, , main_thing.load() execute code? or @ least, similar behavior.

basically, similar default constructor in other languages don't need call main_thing.constructor().

if isn't possible, please explain bit of detail.

like tom tu said, functions objects, , can have properties...

var main_thing = {      // load set result of anonymous function     // function 2 properties set other functions             load: function() {         // create load() function , store in "all"         var = function () {                 // when function executed these have been assigned                all.load1();                all.load2();             };          // set 2 properties sub load functions         all.load1 = function() {};         all.load2 = function() {};          // return our function         return all;     }() }  main_thing.load(); // or  main_thing.load.load1(); main_thing.load.load2(); 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -