Hi guys, Sorry I bring you guys a head-spinning question regularly. I'm on a quest to build a better world with JavaScript. LOL. Really, I mean. Ok, how do I choose to fire specific function from the code below: function x(obj){ Object.keys(obj).forEach(function (fn) { if (typeof obj[fn] === 'function') { try { return obj[fn].apply(this, arguments); } catch (ex) { console.log(fn, "(): " + ex.message); } } }); } var a = { b: function() { console.log("Function B fired!"); }, c: function() { console.log("Function C fired!"); } }; x(a, 'b'); Code (markup): Why I want to do this? In a long chain of data processing, functions fired through this method will continue running without interruption if one of them has an error. Hence improves the user experience since the function that has error would fail silently. Without this, if function b has error function c would never fire and the whole process stops. I have a train-long and messy data processing process so this is very important to me. Thank you,
I'm not certain I follow your question -- you don't have code for the second parameter, you are passing anonymous functions to run all of them... Just what is it you're trying to do with this?!? But as I often say, what's the data and what are you trying to do with it? "Placeholder" garbage like this is near impossible to decipher much less recommend better ways of handling; it's like CSS without the markup it's pointing at, might as well be Klingon. Though I have the NASTY feeling you're getting too complex for your own good whatever it is this is supposed to be doing.
Sure it doesn't. Because that is not the correct way to fire function b or function c. I just can't imagine how to do it the right way. If you try: x(a); Code (markup): Bang! All of them fired and that's not what I want. I just want function b or c fires at a time. I don't want these: a.b(); || a.c(); || a['b'](); || a['c'](); Code (markup): I closest shot I got look like this: // shorthand function x(obj){ for(prop in obj){ if(typeof obj[prop] === 'function'){ obj[prop].apply(this, arguments) } } } var a = { b: function() { console.log("Function B fired!"); }, c: function() { console.log("Function C fired!"); } }; x(a); Code (markup): Then I was stumped. It's okay if I can't achieve this because it would just add perfection to my code scheme, but it would be great if I can do it. This don't really add drawback to performance. I'd jsperf it. Before asking, I've done pretty much research (Googling) already. I just hope someone point me directions and I'll find the way to save the galaxy
Why not just call a.b(); rather than pass it? Perfectly good object, call it directy! If you only want one of it's methods, call that method rather than having some goofy wrapper. The only reason to even HAVE that wrapper is if you want to call all it's methods. I swear, it's like people are trying to automate things to be MORE complex now... kinda defeats the point of automating things.
I think I'm still not getting what it is you're even trying to do. The needlessly vague and generic code and method names could be contributing to that. Your edited post is even more gibberish in that your methods aren't returning boolean, so what are you even expecting those "or" operations to do? What do you actually mean by "fires at a time"?