是的,将所有 JavaScript 声明放在顶部是一个很好的做法。让我们看一个例子 –
示例
<html>
<head>
<title>JavaScript String match() Method</title>
</head>
<body>
<script>
// all the variables declared at top
var str, re, found;
str = "For more information, see Chapter 3.4.5.1";
re = /(chapter \d+(\.\d)*)/i;
found = str.match( re );
document.write(found );
</script>
</body>
</html>
登录后复制
以下是有效的原因 –
- 提供一个地方来检查所有变量。有助于避免全局变量 li>避免重新声明。代码也易于其他人阅读。
以上就是在JavaScript中,将所有声明放在顶部是一种好的做法吗?的详细内容,更多请关注www.92cms.cn其它相关文章!






