Friday, 15 May 2015

javascript - Callback inside nested function -



javascript - Callback inside nested function -

i'm trying understand this code

i have simplified code below , trying understand how utilize function callback.

function test(text, callback) { function test2() { callback(text); } }

and phone call with

test('sometext', function(response) { console.log(response); });

however test2 never gets called.

how can phone call test2 callback?

you need phone call text2. @ moment define it. there various ways this. simplest phone call function is:

function test(text, callback) { (function test2() { callback(text); })(); }

note (); @ end of definition , parentheses around it. phone call test2 when test run.

another way same thing phone call after defining it:

function test(text, callback) { function test2() { callback(text); } test2(); }

alternatively, can utilize test mill test2 , homecoming , phone call @ later date:

function test(text, callback) { homecoming function test2() { callback(text); } } var f = test('logged', console.log); f();

in illustration link not clear me how callback called because doesn't gettokenandxhr ever called. perhaps there magic going on in chrome browser calls via reflection of somekind.

javascript callback

No comments:

Post a Comment