javascript - 不同的 JavaScript block 类型

标签 javascript

有什么区别:

例 1。

<script type="text/javascript">   
document.write("<h1>This is a heading</h1>");   
document.write("<p>This is a paragraph.</p>");   
document.write("<p>This is another paragraph.</p>");   
</script>  

例 2。

<script type="text/javascript">   
{   
    document.write("<h1>This is a heading</h1>");   
    document.write("<p>This is a paragraph.</p>");   
    document.write("<p>This is another paragraph.</p>");   
}   
</script>

W3C 学校表示:

Ex 1. Each statement is executed by the browser in the sequence they are written.
Ex 2. The purpose of a block is to make the sequence of statements execute together.

请解释一下上述说法。浏览器对上述命令有何不同的 react ?

最佳答案

完全没有区别, block 的目的是在 ifs 和 fors 中使用,如下所示:

示例 1 如果 () //要执行的命令

示例 2 如果 () { //要执行的许多命令 }

在第二个示例中,if 语句将 block 命令“视为”单个命令,在 block 内,命令仍然按顺序执行。

关于javascript - 不同的 JavaScript block 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6520594/

相关文章:

javascript - 改变 JSON 对象的 React 状态 将数组存储为值

javascript - 使用node.js连接socket.io

javascript - 删除 URL 开头和结尾的斜杠

javascript - 滚动仅在 Android Chrome 上不起作用

javascript - 为什么此代码返回 3 维数组

javascript - 清除 Nightmare.js (Electron) 中的缓存

javascript - 仅在满足条件时填充

javascript - Chrome 应用程序与主页之间的通信与 PostMessage

javascript - 动态句子中的单词倒序

javascript - 将 debounce 与 react 一起使用不会触发功能