0

I don't know if this is possible, but what I'd like to accomplish is assign a property value to an object from a function that is called. When the function for active is ran, I'd like to assign either a true or false as its value as the object is being created. Can we use self-executing functions in this way?

Here's my code, still in-progress, but is there something of this sort that is accomplish able in javascript?

        graphObj.list.push({
            name: graphData[i].name,
            value: Math.floor(graphData[i].time / graphData[graphData.length - 1] * 100),
            str: graphData[i].strTime
            active: (function(){
                if (activeActivityName != "" && graphData[i].name == activeActivityName){
                    return true;
                } else {
                    return false;
                }
            })
        });
4
  • 1
    This is not a self-invoking function. You would need () at the end for it to be immediately executed. Commented Mar 21, 2015 at 2:19
  • Adding a () causes a syntax error, it appears as that isn't the proper syntax if this is possible Commented Mar 21, 2015 at 2:21
  • Works fine for me: jsfiddle.net/s2kwn2h3 Commented Mar 21, 2015 at 2:23
  • 2
    Oh, you're missing a comma after strTime. Commented Mar 21, 2015 at 2:25

2 Answers 2

2

I really want to discourage you from using the IIFE here since I strongly believe this is an inappropriate use of IIFE. So how about just assigning the result of the condition:

graphObj.list.push({
    name: graphData[i].name,
    value: Math.floor(graphData[i].time / graphData[graphData.length - 1] * 100),
    str: graphData[i].strTime,
    active: (activeActivityName != "" && graphData[i].name == activeActivityName)
});
Sign up to request clarification or add additional context in comments.

1 Comment

I do like this approach!
0

Why go down the path of using an Immediately-Invoked Function Expression when you could just call a function.

function isActive(activeActivityName, graphData, i) {
    if (activeActivityName != "" && graphData[i].name == activeActivityName) {
        return true;
    } else {
        return false;
    }
}

graphObj.list.push({
    name: graphData[i].name,
    value: Math.floor(graphData[i].time / graphData[graphData.length - 1] * 100),
    str: graphData[i].strTime,
    active: isActive(activeActivityName, graphData, i)
});

3 Comments

I know I'm only going to be using the function in one place, so I thought an IIFE would be best here. Your example is also fine
An anonymous function is fine in that case... so long as it doesn't overcomplicate your code.
That is something of a matter of opinion. Consider this rebuttal. toddmotto.com/avoiding-anonymous-javascript-functions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.