Does a JavaScript Anonymous function have access to other parameters passed along with it? -


let's have function takes 2 parameters, regular variable , function.

function example(vara, function(){   //do vara? }) 

can use vara in definition of anonymous function? if run function , pass in vara, anonymous function know vale of vara is?

answer no!

your definition incorrect.

function example(vara, function(){   //do vara? }); 

during function definition, not know callback , should be:

function example(vara, callback){} 

now in following example:

function test(vara){    function notify(){      console.log(vara);    }    notify();  }    test(10)

vara accessible because in same scope, if this:

function test(vara, callback){    callback();  }    test(10, function(){        // throw error, because there no variable called vara    console.log(vara);  })

and have pass arguments callback.

function test(vara, callback){    callback(vara);  }    test(10, function(vara){    console.log(vara);  })


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -