【发布时间】:2021-06-11 16:48:51
【问题描述】:
查看 JSlint 上的所有信息(例如,here 或 here),讨论是这样的
始终使用严格模式,因为它会捕获更多错误。您所要做的就是将“使用严格”放在代码的顶部。而已!再简单不过了...除非 JSlint 抱怨并告诉您将其放入函数范围内,否则您必须将所有代码放入一个巨大的函数中。
我有数百个用 javascript 编写的函数,至少 <body>在加载时调用了一个回调函数,所以我试图弄清楚我应该如何将所有函数放入另一个函数中,但仍要确保<body> 元素仍然可以刺穿外部函数并在内部范围内调用我的函数。
谁能告诉我如何为这样的事情实施严格模式
function foo(){
"use strict"; /* Approach 1: Putting inside every function */
/* Extremely tedious and ugly */
$('#example').text('Hello '+ bar());
}
function bar(){
"use strict"; /* Approach 1: Putting inside every function */
/* Extremely tedious and ugly */
return 'Beautiful';
}
function fooBar (){
"use strict"; /* Approach 2: Putting once at the top of an outer function */
/* But now how does <body> call this function? */
function foo(){
"use strict";
$('#example').text('Hello '+ bar());
}
function bar(){
"use strict";
return 'Beautiful';
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body onload="foo();">
<p id='example'>
</p>
</body>
【问题讨论】:
标签: javascript ecmascript-6 scope jshint