0

P.S.: I am not asking how to pass a function to a function! I am asking how to add a argument after passing a function.

I want to pass functionA as a variable to functionB, and add a extra object to functionA inside functionB. How can I do this?

function functionA() {
    console.log( arguments );
}
function functionB( fn ) {
    fn( {
        c : 'extra object',
    });
}
functionB( function() {
    functionA( {
        a : 'foo',
        b : 'bar',
    })
});

this is the result:

{ '0': { a: 'foo', b: 'bar' } }

but what I want is:

{ '0': { a: 'foo', b: 'bar', c: 'extra object' } }

1 Answers1

0

try this

function functionA() {
    console.log( arguments );
}
function functionB( fn ) {
    fn({"d": "c"}); //passing the extra arguments from here
}
functionB( function() {  
    var obj ={
        a : 'foo',
        b : 'bar'
    }; 
    obj.extra = arguments; //adding the arguments to function A
    functionA( obj );
});
gurvinder372
  • 66,980
  • 10
  • 72
  • 94