我在父函数中有一个内部函数。
我想我明白这是如何工作的:http://jsfiddle.net/93pp5/
var text = 'Look at me';
function parent_function() {
function inner_function() {
var scream = '!!!';
alert(text);
}
inner_function();
};
parent_function();
但是,我试图找出为什么下面的 javascript 不起作用。 parent_function()不能返回inner_function()吗?我的期望是在底部调用parent_function() 会发出警报,但它不会:http://jsfiddle.net/93pp5/1/
var text = 'Look at me';
function parent_function() {
return function inner_function() {
var scream = '!!!';
alert(text);
}
};
parent_function();
最佳答案
那是因为 parent_function
返回一个仍需要调用(执行)的函数。
var runAlert = parent_function();
// runAlert now holds the returned function but it still needs to be called to get the alert
runAlert(); // will alert
关于javascript - 了解当父函数返回内部函数时 javascript 如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24029017/