Understanding how javascript works when a parent function returns an inner function -
i have inner_function inside parent_function.
i think understand how works: http://jsfiddle.net/93pp5/
var text = 'look @ me'; function parent_function() { function inner_function() { var scream = '!!!'; alert(text); } inner_function(); }; parent_function();
however, i'm trying figure out why javascript below not work. can parent_function() not return inner_function()? expectation calling parent_function() @ bottom bring alert, not: http://jsfiddle.net/93pp5/1/
var text = 'look @ me'; function parent_function() { return function inner_function() { var scream = '!!!'; alert(text); } }; parent_function();
thats because parent_function
returns function still needs called (executed).
var runalert = parent_function(); // runalert holds returned function still needs called alert runalert(); // alert
Comments
Post a Comment