cuốn sách gpt4 ai đã làm

javascript - setInterval ("/*some code*/", time) 和 setInterval(function() {/*一些代码*/}, time)

In lại Tác giả: Walker 123 更新时间:2023-11-28 11:18:14 28 4
mua khóa gpt4 Nike

我的印象是

setInterval("/*some code*/", time)

相当于

setInterval(function() {
/*some code*/
}, time)

显然不是!请比较以下内容(完整 HTML):



这两个动画(第一个动画取自 đây )是不同的。

为什么这两个构造不等价?

答案:至少存在三个差异

  1. 变量范围
  2. 性能
  3. 字符串字符转义

câu trả lời hay nhất

我还没有研究过混淆的代码,但是使用字符串和带有 setTimeout hoặc setInterval 的函数之间的区别在于代码运行的范围。另外,当您使用字符串时,某些引擎可能也无法优化。

  • 当您传入字符串时,它会在全局范围内运行。

  • 当您传入一个函数时,它会在定义它的范围内运行。

这会影响哪些变量在代码范围内,从而影响代码的功能。

这是一个未混淆的示例:Live copy | source

(chức năng() {
var foo = 42;

// This will say "undefined", because there is no global `foo`
setTimeout("display(typeof foo);", 0);

// This will say "number", because the function is closure over
// the current scope, whcih *does* have `foo`
setTimeout(function() {
display(typeof foo);
}, 0);

})();

function display(msg) {
var p = document.createElement('p');
p.innerHTML = String(msg);
document.body.appendChild(p);
}

关于javascript - setInterval ("/*some code*/", time) 和 setInterval(function() {/*一些代码*/}, time),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10364235/

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress