0

Fiddle here

I'm looking at making some very high level logic into helper functions for curiosity's sake. I would like to be able to execute a function with its parameters in the _if function without having to define something like _callback ahead of time? I feel like I'm missing something here.

var _if = function(predicate,callback){
    if(predicate){
        callback(); //callback(arguments) is not arguments for callback
    }
};
var text = 'some text';
_if(1 > 0,function(){
    console.log('hello world'); //hello world
});
_if(1 > 0,function(text){
    console.log(text); //undefined
});
//define callback for this situation
var _callback = function(x){
    console.log(x);
}
_if(1 > 0,function(){
    _callback(text); //some text
});

2 Answers 2

1

Not sure what you want, but maybe this helps:

You can call your function like this too:

_if(1 > 0,_callback.bind(null,text));  //null is the value of this

_if(1,function(text){
    console.log(text); //this way not undefined
}.bind(null,text));

1) This works because of the logic of JavaScript. In further versions you can use your version too if you use let text = 'some text'; - ref

2) null is the this value for the function, (more info here) but I think you can pass this too if you need to use it in the function (null will be automatically replaced in non-strict mode to the global object. That's why you can use window.colsole.log inside the function. - ref)

Sign up to request clarification or add additional context in comments.

2 Comments

i'm looking for a way to get at the arguments object of the callback function in the _if function, without having to define _callback beforehand. In the second call to _if you can really see what i'm trying to get at. that function prints undefined when i pass in parameters to the callback.
thanks, that's what i was looking for. two questions: 1.) why does this work, 2.) why pass null instead of this? as the context?
1

Why not just take the arguments to the callback function as extra parameters on _if?

var _if = function (predicate, callback, context) {
    if (predicate) {
        var callbackArgs = [].slice.call(arguments, 3);
        callback.apply(context || null, callbackArgs);
    }
};

// Usage:
_if(true, console.log, console, "text");

5 Comments

because i knew that way would work and i was looking for reasons why i hadn't seen what i asked about :)
why pass null at the context to apply?
You didn't specify you wanted a calling context as well. You could easily have a 3rd named argument for the context and change the slice argument to 3, and that would work. With some clever thinking about your argument list, you could probably even find a way to require only one function (which can deduce if a calling context was passed in or not).
Oops, never mind, now I see. It wasn't clear that your question was specifically about this. I'll modify my answer.
i think i actually like this better than what i was going after. it reads nicely to have the parameters meant for the callback passed last.

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.