简体   繁体   English

未捕获的ReferenceError:未定义xyz

[英]Uncaught ReferenceError: xyz is not defined

why do I get a not defined error when defining this function with single quotes? 在使用单引号定义此函数时,为什么会出现未定义的错误?

example: 例:

rotator.doStuff =  function(num) {
        //do stuff
        rotator.timer = setTimeout('rotator.doStuff('+num+')',500);
}

Assuming that this is the contents of an IIF. 假设这是IIF的内容。

When using a string, rotator will be resolved in the global scope, and it won't be available there, thus the error. 使用字符串时, rotator将在全局范围内解析,并且在那里不可用,因此出错。

You should try this. 你应该试试这个。 Now rotator will resolved in lexical scope, from which it is accessible. 现在, rotator将在词法范围内解决,从中可以访问它。

rotator.timer = setTimeout(rotator.doStuff.bind(rotator, num), 500);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM