spscwork Posted September 23, 2012 Posted September 23, 2012 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
Shinryuu Posted September 24, 2012 Posted September 24, 2012 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.
spscwork Posted September 25, 2012 Author Posted September 25, 2012 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.
Shinryuu Posted September 25, 2012 Posted September 25, 2012 Ah.. keep us updated will you? I'm sure many web devs in the community would probably like this.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now