gpt4 book ai didi

javascript - ECMAScript : Some questions about Job and Job queue

转载 作者:行者123 更新时间:2023-11-29 16:36:54 27 4
gpt4 key购买 nike

这个关于 ECMAScript 规范(ECMA-262 第 8 版)的问题

这些天,我对作业和作业队列有点困惑。

这里有一些问题。

1:在ECMA-262中,有两种作业队列。一个是 ScriptJobs,另一个是 PromiseJobs。那么,女巫一号有偏好吗?
2:在ECMA-262中,仅定义了RunJobs抽象操作。我想知道 RunJobs 执行的时间和地点?
3.我在FF 60中执行了blow代码。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
window.addEventListener('DOMContentLoaded', () => {
let x = document.createElement('script');
x.innerHTML = 'console.log(3);';
console.log('script start');
document.body.appendChild(x);
setTimeout(() => console.log(1), 0);
Promise.resolve(2).then(console.log);
console.log('script is end');
});
</script>
</head>
<body>
</body>
</html>

它记录:

script start
3
script end
2
1

为什么 script 元素在动态创建的执行上下文上执行?

c. Let nextQueue be a non‐empty Job Queue chosen in an implementation‐defined manner. If all Job Queues are empty, the result is implementation‐defined.

从 ECMA-262 RunJobs fork 。HTML 规范如何定义实现定义的方式

@Bergi 像这样吗? PromiseJobs : [] , ScriptJobs : []

ScriptJobs.push(something);   
// pop and run by Event loop
// blow will happens while `something` runs
PromiseJobs.push(anotherOne);
ScriptJobs.push(theother);
//end of `something`
//and in here, PromiseJobs will pop?

最佳答案

Which one have preference?

都不是。规范states该规范没有定义多个作业队列的服务顺序。”队列只是一个工具,用于指定每个队列按 FIFO 顺序执行作业.

I want to know when and where the RunJobs execute?

这在很大程度上取决于实现。在像 Node.js 这样的环境中,它将在进程启动时被调用 - 这是基本的事件循环公式。当然会有更多的作业队列,例如用于定时器或异步IO操作。

Why script element execute on execution context that created dynamically?

因为 HTML 规范很奇怪。看看https://www.html5rocks.com/en/tutorials/speed/script-loading/load and execute order of scripts以获得概述。详情可见 HTML5 processing model for script elements ,基本上就是这样

  • 当脚本元素连接时(即插入到 DOM 中),
  • 准备好一切并加载(如果需要)并进行各种检查,
  • 如果该元素没有 src 属性并且是经典脚本(不是模块)并且未插入解析器,
  • 然后“立即执行脚本 block ,即使其他脚本已经在执行。

是的,这使浏览器的 ECMAScript 队列模型完全失效。 Integration with the JavaScript job queue 部分也明确说明了这一点.

关于javascript - ECMAScript : Some questions about Job and Job queue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50373175/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com