1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to execute particular function that is running through this function

Discussion in 'JavaScript' started by ketting00, Dec 2, 2014.

  1. #1
    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,
     
    Last edited: Dec 2, 2014
    ketting00, Dec 2, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    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.
     
    deathshadow, Dec 3, 2014 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #3
    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 :)
     
    Last edited: Dec 3, 2014
    ketting00, Dec 3, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    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.
     
    deathshadow, Dec 3, 2014 IP
  5. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #5
    I've edited the thread above. You see, it not easy. It's okay if I failed.
     
    ketting00, Dec 3, 2014 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    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"?
     
    deathshadow, Dec 3, 2014 IP