0

I will walk across the scenario about what i am trying to do:

I pass a function with parameter as a parameter to a function. This function will add an additional parameter to the passed function and then make a call to a function.

For ex:

funcA(function(){ funcB("add Me") })    // call to funcA

funcA : function( fn ){ 
 var additionalParams = "add Me too"
}

funcB : function( str1,str2){ //gets me both strings }

I looked here and thought this is what i wanted but did not help.

Here is the jsfiddle i tried.

Community
  • 1
  • 1
inputError
  • 600
  • 3
  • 12
  • 26

1 Answers1

1

is not necessary call apply method. just declare an parameter on function passed to funcA

funcA = function( fn ){ 
    var additionalParams = "add Me too";
    fn(additionalParams); 
}

funcA(function(str){
    funcB("add Me", str);
});

funcB = function( str1,str2){ //gets me both strings };

update

your jsfiddle code modified like i said.

function appendArguments(fn) {
    var slice = Array.prototype.slice.call.bind(Array.prototype.slice),
        args = slice(arguments, 1);
    return function (t) {
        return fn(t);
    };
}

var bar = appendArguments(function(t){foo(1, t)});
bar(2);



function foo (x,y){
    alert(x);
    alert(y);
}

try yourself. it works for me.

Callebe
  • 1,087
  • 7
  • 18
  • I havent posted the actual problem but what u are suggesting cant be done as far as i know. isnt there a way the problem cant be solved? – inputError Aug 15 '14 at 01:00
  • Whay what i suggested can`t be done? of course it can. – Callebe Aug 15 '14 at 01:03
  • Sorry my bad! Yh looks like this should work. I will give it a try on the actual problem and let you know. Thanks.... Btw what does parameter t in anonymous function do? – inputError Aug 15 '14 at 01:11
  • anonymous function is like any other function, it can receive parameters. the fuctions is expecting to receive a parameter... just this. – Callebe Aug 15 '14 at 01:15