Javascript Object: Map of Functions. How to return default value -
sorry title of question, don't found better one.
introduction
my goal create object, , associate every params function. simple!
var canidothis = { one: function(a, b, c){}, two: function(a){}, three: function(){} } i want call canidothis in way
if(canidothis['one'](x, y, z)) { // somthing } if(canidothis['two'](w)) { // somthing } if(canidothis['three']()) { // somthing } if have var can "one", "two" or "three" can use canidothis in way
// myvariable can equalso "one" or "two" or "three" var myvarable; if(canidothis[myvarable]()) { // somthing } my problem
i'd manage myvarable value. if call canidothis['four']() uncaught typeerror: undefined not function
my question
is there way prevent default behaviour of uncaught typeerror , return default value? cute if canidothis['four']() interpretated false or undefined
ps: in pure javascript ;)
try checking if property function before calling using typeof.
if (typeof(canidothis[myvarable]) === 'function') { canidothis[myvarable](); }
Comments
Post a Comment