[英]Function inside function doesn't work
I want to call a function, which has a function inside it.我想调用一个函数,它里面有一个函数。 Why does this code not work?
为什么这段代码不起作用? It has to look like this, because I've created a XMLHttpRequest inside getNumber().
它必须看起来像这样,因为我在 getNumber() 中创建了一个 XMLHttpRequest。 The code below is just an example.
下面的代码只是一个例子。
getNumber();
function getNumber()
{
otherFunction = function()
{
console.log("It's working!");
}
}
It does "work" (well, so long as you aren't using strict mode — always use strict mode! — since it depends on implicit globals).它确实“有效”(好吧,只要你不使用严格模式——总是使用严格模式!——因为它依赖于隐式全局变量)。
You call getNumber
, which defines a global variable called otherFunction
and uses a function expression to assign a value to it.您调用
getNumber
,它定义了一个名为otherFunction
的全局变量并使用函数表达式为其赋值。
If you ever called otherFunction
( you don't! ), it would log It's working .如果你曾经调用过
otherFunction
(你没有! ),它会记录It's working 。
As it stands, nothing in your code should generate any output.就目前而言,您的代码中的任何内容都不应生成任何输出。
getNumber(); function getNumber() { otherFunction = function() { console.log("It's working!"); } } otherFunction();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.