0

Is there a way how to create a default method call when the method that is called from object is undefined?

e.g.

myObj = {
   default: function(){ 
      console.log("You've called method that isn't defined");
   },
   sayHi: function(){ 
      console.log("myObj says Hi!");
   }
}

//console output: "myObj says Hi!"
myObj.sayHi();

//console output: "You've called method that isn't defined"
myObj.methodThatIsNotReallyThere(); 
jave.web
  • 13,880
  • 12
  • 91
  • 125
  • As a workaround, you can define a method called `.call('name')` and use the string to look up a method in in an object used as a dictionary. If the named method is undefined, then .call could execute something else. – Paul Mar 17 '15 at 00:50
  • @Paul thanks for suggestion, however I can not alter the code using this object :) – jave.web Mar 17 '15 at 00:51

0 Answers0