Jump to content

Respond To An Unknown Function


spscwork

Recommended Posts

Hello,

I am trying to figure out a way to get a default function that is called if a function does not exist. For example

 

var foo = {
bar: function(){
 console.log("Bar was called")
},
unknown: function(){
 console.log("Unknown was called")
}
}
foo.bar(); // Should log "Bar was called"
foo.biz(); // Should log "Unknown was called"

 

Is there any way to do this cross browser, and if not then is there a way to do this in certain browsers?

 

Thank you

Link to comment
Share on other sites

The trick is to create a script that searches for the function keyword which defines the, backtrack a bit to the start of the line to find the function's name (search after a "\n" and before ":") and have the function log an unknown function call was made when it finds no :, but does find \n. That's if you're asking for an anonymous function being called, if you're asking about a function that doesn't exist being called, do the above to parse the source file for functions and then append each found function name to an array. When a function call is made check the function called against this array before allowing the execution, I'm not sure where this would ever be needed, but that's the rough algorithm. I can make something in Ruby which can do these things but not in js since I'm a total newb at that.

Link to comment
Share on other sites

Thank You,

 

Just so you know, I'm creating a HTML5 Canvas Library called Hotspots which allows the web developer to define a section of the canvas which is responsive to events and, unlike others that I have found, is responsive to canvas transformations. so you can rotate, scale, translate and skew the canvas and align the Hotspots easily.

 

Thank you again.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...