[英]Function inside function doesn't work
我想调用一个函数,它里面有一个函数。 为什么这段代码不起作用? 它必须看起来像这样,因为我在 getNumber() 中创建了一个 XMLHttpRequest。 下面的代码只是一个例子。
getNumber();
function getNumber()
{
otherFunction = function()
{
console.log("It's working!");
}
}
它确实“有效”(好吧,只要你不使用严格模式——总是使用严格模式!——因为它依赖于隐式全局变量)。
您调用getNumber
,它定义了一个名为otherFunction
的全局变量并使用函数表达式为其赋值。
如果你曾经调用过otherFunction
(你没有! ),它会记录It's working 。
就目前而言,您的代码中的任何内容都不应生成任何输出。
getNumber(); function getNumber() { otherFunction = function() { console.log("It's working!"); } } otherFunction();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.