普通javascript是否在node-webkit上工作?
我在node-webkit中使用了普通javascript,例如: 我得到的错误如下:普通javascript是否在node-webkit上工作?,javascript,node.js,Javascript,Node.js,我在node-webkit中使用了普通javascript,例如: 我得到的错误如下: Uncaught node.js Error RangeError:超过最大调用堆栈大小 在asd(file:///C:/Users/Humlal/Downloads/Compressed/node-webkit-v0.10.5-win-ia32/index.html:5:13) 在asd(file:///C:/Users/Humlal/Downloads/Compressed/node-webkit-v
Uncaught node.js Error RangeError:超过最大调用堆栈大小 在asd(file:///C:/Users/Humlal/Downloads/Compressed/node-webkit-v0.10.5-win-ia32/index.html:5:13) 在asd(file:///C:/Users/Humlal/Downloads/Compressed/node-webkit-v0.10.5-win-ia32/index.html:14:13)在asd(file:///C:/Users/Humlal/Downloads/Compressed/node-webkit-v0.10.5-win-ia32/index.html:14:13)
是的,普通Javascript在节点webkit上工作 Node运行JavaScriptV8引擎,因此任何JS函数都可以在Node中理解 您的错误如下:
var asd = document.title; //This is a string
setTimeout(asd(),1000); //You call asd as a function, which does not exist, hence the error
编辑:
使用新变量名时,应该可以:
var blinkOrder = 20;
var blinkNumber = 12;
var asdf = document.title;
function asd(){
if (blinkNumber >= blinkOrder) {
document.title = ' hi';
blinkNumber = 0;
}
else{
document.title = asdf;
blinkNumber++;
setTimeout(asd(),1000);
}
}
asd();
尽管我理解反对票,但我想补充一点: 小心起吊() 函数和变量被提升,但函数在提升中优先于JS,因此在代码中,首先为
asd
分配函数,然后为document.title值
console.log(asd); // asd(), because even if the function is defined further
// in the code, it is hoisted
var asd = document.title;
console.log(asd); // document.title value, so the string of the page title
function asd(){
if (blinkNumber >= blinkOrder) {
document.title = ' hi';
blinkNumber = 0;
}
else{
document.title = asd; // Why ??!?!
blinkNumber++;
}
setTimeout(asd(),1000); // setTimeout() is expecting a function reference!
}
asd(); // Here, asd has been assigned the document.title value, and a string, not being a function, cannot be invoked, thus the error thrown.
错误源于这样一个事实,
asd
是一个包含document.title
的字符串。虽然我将asd=document.title
更改为asdf=document.title
,但我得到了相同的错误。我纠正了这个错误。。。将var asd更改为asdf,然后再次出现如下错误:>Uncaught node.js error RangeError:asd超过了最大调用堆栈大小(file:///C:/Users/Humlal/Downloads/Compressed/node-asd的webkit-v0.10.5-win-ia32/index.html:5:13)(file:///C:/Users/Humlal/Downloads/Compressed/node-webkit-v0.10.5-win-ia32/index.html:14:13)在asd(file:///C:/Users/Humlal/Downloads/Compressed/node-webkit-v0.10.5-win-ia32/index.html:14:13)函数setTimeout(reference,time_in_ms)
需要一个引用,而您给出的是asd()函数的结果,因此您无限期地递归调用asd()函数。@laruiss哈哈,谢谢,@xShirase,你让我开心!;-)setTimeout将无限期启动。看看我的例子,如果你在else子句中设置它,它只会在你的条件满足之前触发,然后停止,一切都会好起来:-)OP已经更正了他的代码,但没有更正他的帖子,请查看我答案的注释。(WTF不是建设性的批评)@xShirase你是对的,纠正了我的回答
console.log(asd); // asd(), because even if the function is defined further
// in the code, it is hoisted
var asd = document.title;
console.log(asd); // document.title value, so the string of the page title
function asd(){
if (blinkNumber >= blinkOrder) {
document.title = ' hi';
blinkNumber = 0;
}
else{
document.title = asd; // Why ??!?!
blinkNumber++;
}
setTimeout(asd(),1000); // setTimeout() is expecting a function reference!
}
asd(); // Here, asd has been assigned the document.title value, and a string, not being a function, cannot be invoked, thus the error thrown.