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(); 
2
  • 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. Commented Mar 17, 2015 at 0:50
  • @Paul thanks for suggestion, however I can not alter the code using this object :) Commented Mar 17, 2015 at 0:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.