是的,是的,我知道,严格模式还没有,但实际上,我正在为未来做准备......
那么,为什么这样:
$('#'+ $(this).attr('id').replace('control-', 'legend-')).fadeIn();
Run Code Online (Sandbox Code Playgroud)
...在ES5严格模式下不允许?
还是我误解了?JSLint的:
Problem at line 516 character 18: Strict violation.
可能会更冗长一点,我想......?
编辑:
为避免混淆,这里有更多的原始代码:
function displayLegend() {
$('#'+ $(this).attr('id').replace('control-', 'legend-')).fadeIn();
}
Run Code Online (Sandbox Code Playgroud) 暂且不谈源于代码结构变化的可能差异"use strict";,在严格模式下运行代码是否提供任何速度优势?
我正在努力获得"严格使用"; 工作的指示,并有一点麻烦.在下面的文件中,FireFox 9将(正确地)检测到someVar尚未在第3行声明,但未能检测到第17行尚未声明theVar.我很难理解为什么会出现这种情况.
"use strict"; // this will cause the browser to check for errors more aggresively
someVar = 10; // this DOES get caught // LINE 3
// debugger; // this will cause FireBug to open at the bottom of the page/window
// it will also cause the debugger to stop at this line
// Yep, using jQuery & anonymous functions
$(document).ready( function(){
alert("document is done loading, but not (necessarily) the images!");
$("#btnToClick").click( function () {
alert("About to stop"); …Run Code Online (Sandbox Code Playgroud) 参考:http://ejohn.org/blog/simple-class-instantiation/
// makeClass - By John Resig (MIT Licensed)
function makeClass(){
return function(args){
if ( this instanceof arguments.callee ) {
if ( typeof this.init == "function" )
this.init.apply( this, args.callee ? args : arguments );
} else
return new arguments.callee( arguments );
};
}
Run Code Online (Sandbox Code Playgroud)
我想知道,如果有任何符合ECMAScript 5的方式来实现相同的功能.问题是,访问arguments.callee在严格模式下已弃用.
如果我有以下内容
"use strict";
$(document).ready(function () {
});
Run Code Online (Sandbox Code Playgroud)
我收到了警告
'$'is not defined
Run Code Online (Sandbox Code Playgroud) 我想知道是否真的有必要包括"use strict"当我完成编程并将我的JavaScript文档发布给任何人看.我喜欢使用它,因为要检查我是否以良好的方式编码.
那么,"use strict"当我为公众发布我的JavaScript文件时,是否应该包含或仅删除使用?
我问的原因是为了节省JavaScript文件中的空间.
请假设'使用严格'; 并且还假设,JSLint已启用且错误不能被忽略.
我发现运算符和','启动列表更具可读性,
例如:
var i = 0
, j = 1
, someLongVariablename1
, someLongVariablename2
, someLongVariablename3
, someLongVariablename4;
if( (
'dcr' === cmd
&& (action)
&& ('get' === actionHttp || 'post' === actionHttp )
&& whatever
) { ... }
Run Code Online (Sandbox Code Playgroud)
因此我的问题
是:"使用严格"是否"坏线断裂"已经过时了?
编辑:'使用严格'; 不会阻止执行坏线破坏代码.它可以防止执行某些错误.
我看到JSLint和JSHint以不同的方式处理坏的断行.JSHint对我喜欢的语法更友好.
因此,对于正在研究此问题的其他人而言,这可能是一种解决方案.
JSLint给出了"严格违规"错误,尽管我在一个隐藏全局范围的函数中使用了"this"上下文.
function test() {
"use strict";
this.a = "b";
}
Run Code Online (Sandbox Code Playgroud)
为了记录,我在Webstorm中使用内置的JSLint解析器.
这是我的代码似乎表明答案是肯定的 - http://jsfiddle.net/4nKqu/
var Foo = function() {
'use strict'
return {
foo: function() {
a = 10
alert('a = ' + a)
}
}
}()
try {
Foo.foo()
} catch (e) {
alert(e)
}
Run Code Online (Sandbox Code Playgroud)
您能否引用标准中的陈述,阐明该陈述'use strict'是否自动应用于我们应用的函数中定义的所有闭包和函数'use strict'?
我发现了一个有趣的案例,其中"use strict"在javascript中无法正常工作.以下功能
"use strict";
var y = () => {
console.log(this);
}
var x = function () {
console.log(this);
}
x(); // undefined due to use strict
y(); // window object
Run Code Online (Sandbox Code Playgroud)
我认为胖箭头上下文也应该被未定义覆盖,或者我的假设错了?
javascript ×10
use-strict ×10
ecmascript-5 ×3
strict ×3
jquery ×2
jslint ×2
ecmascript-6 ×1
minify ×1
performance ×1
webstorm ×1