Arbitrary object methods and properties in JavaScript -


i'm sure has definitively been answered before, , i've tried search it.. maybe search terms wrong...

basically have object myobject, , have set of defined properties , methods it. want able handle calls/references properties , methods have not defined.

for example, let's have this:

var myobject = {   someproperty : 'foobar',   somefunction : function () { /* stuff */ } } 

currently, if tries make call myobject.someotherfunction(), javascript yells , screams it. want setup way automatically handle that. example, instead of javascript throwing error, object returns false. possible?

another way @ this:

var myobject = {   somefunction : function () { /* stuff */ }   magicbucket : function () { /* stuff */ } } 

if call myobject.somefunction(), defined , something. want happen if instance call myobject.someotherfunction(), instead of javascript throwing error, call myobject.magicbucket().

the reason have client uses third-party library on site. want discontinue using it, removing going take lot of time , effort. short-term solution, wanted know if make dummy file nothing. well, library uses several objects has lots of methods. go through , make dummy objects, thought maybe there might easy "catch-all" method this.

some have mentioned checking if method exists first, wrapping in condition or try..catch, etc. well, point of @ time can't touch actual calls methods. , since overall goal remove coding altogether, it's not applicable.

there's special property called __nosuchmethod__ precisely described. it's non-standard property. works in firefox. here's how use it:

var o = {     __nosuchmethod__: function (name, args) {         alert(name); // prints name of method         alert(args); // prints array of arguments     } };  o.abc(1, 2, 3); // output: abc 1,2,3 

the future proxy objects. following short tutorial on proxies: proxy tutorial


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -