我在jshint中得到了这个,
[L16:C13] W034:不必要的指令“use strict”。
但是我的问题是做这样的工作..
function () {
"use strict";
var a = function () {
//stuff, (is this also strict)
}
}
东西也会严格吗?显然,严格声明之外的事情并不严格。但是其他函数内部是否开始非严格或继承严格性?
答案 0 :(得分:6)
"use strict";
的任何子范围也都是严格的。如果您可以声明封装范围严格,那么我可以看到不需要来声明每个函数 strict 。
我尝试在范围内部以及范围之外调用该函数,以确保它没有任何区别。这是使用mocha
var assert = require('assert');
var inside = function () {
"use strict";
var a = inside.a = function () {
bar = 1;
return bar;
};
var b = inside.b = function () {
var bar = 1;
return bar;
};
var c = inside.c = function () {
"use strict";
bar = 1;
return bar;
};
var d = inside.d = function () {
"use strict";
var bar = 1;
return bar;
};
describe('Inside Context', function () {
it('inside a (no strict, no var)', a )
it('inside b (no strict, var)', b )
it('inside c (strict, no var)', c )
it('inside d (strict, var)', d )
} );
};
inside();
describe('Outside Context', function () {
it('outside a (no strict, no var)', inside.a )
it('outside b (no strict, var)', inside.b )
it('outside c (strict, no var)', inside.c )
it('outside d (strict, var)', inside.d )
} );
在这里我们看到内部和外部没有用var声明变量我们有一个测试失败,表明strict正在工作:
Inside Context
1) inside a (no strict, no var) // fail
✓ inside b (no strict, var)
2) inside c (strict, no var) // fail
✓ inside d (strict, var)
Outside Context
3) outside a (no strict, no var) // fail
✓ outside b (no strict, var)
4) outside c (strict, no var) // fail
✓ outside d (strict, var)