【发布时间】:2014-08-29 19:23:33
【问题描述】:
我使用以下代码非常随机地(大约在 200 次尝试中出现一次)收到“参考错误”。
var securityPrototype = {
init: function(){ /* ... */ },
encryptionKey: function x() {
var i = x.identifier;
return getKey(i);
}
}
securityPrototype.encryptionKey.identifier = Date.now();
function Security(){}
Security.prototype = securityPrototype;
Security.constructor = Security;
function getKey(){ /* ... */ }
var gate = new Security()
gate.encryptionKey(); // Randomly throws : ReferenceError: x is not defined
此代码段位于其他代码中,但没有使用“eval”,也没有使用“with”运算符。
我正在尝试确定是否由于任何情况都可能在此处出现此错误。
重现此内容的浏览器:Mac 和 Windows 上的 Chrome。 IE 和 Safari 运行良好。
【问题讨论】:
-
很可能
Math.random是您的问题,因为您“随机”收到错误。是否允许Math.random的所有值? -
如果是这样的话,我们会得到一个不同的错误。实际上这是可以替换的,因为我试图抽象函数。
-
出现错误和没有出现错误的时间一定有所不同。将
securityPrototype.encryptionKey.identifier设置为固定值,看看是否仍然出现错误。 -
好的,我已经测试了在 Windows 的 Chrome 中运行代码 500 次,我没有收到任何错误。这正是您收到错误时使用的代码吗?
-
@Guffa 几乎是相同的代码。不同之处在于 securityPrototype.encryptionKey.identifier 的值分配不同。主要问题是必须始终解决 encryptionKey 方法中的“x”引用。此功能与我收到的错误相同。
标签: node.js google-chrome javascript javascript-objects