我最近一直在阅读许多JavaScript代码,我想知道使用它有什么好处"use strict"
.任何想法都将非常感激.
"严格使用"; 看起来很棒,我们真的很想在我们的商店使用它.但是,我们只是想要它,以便我们(开发人员)可以找到严格性问题; 我们非常不希望我们的实际客户在以前工作正常的时候让我们的网站中断.
现在,我们可以使用一些服务器端逻辑来实现这一点:
{% if debug %}<script>"use strict";</script>{% endif %}
Run Code Online (Sandbox Code Playgroud)
...除了"use strict"在逐个文件的基础上运行,因此实际上不会工作(好吧,除非我们启动服务器端处理我们所有的JS文件).
所以,我的问题是:在页面加载时执行所有"使用严格"检查以获取检查,或者在页面加载后"use strict"是否可以找到错误? 如果它是前者,我们可以使用"use strict"并停止担心,因为我们会在开发之前加载我们的网站,然后再加载它.但是,如果是后者,我们似乎运气不好,因为我们无法测试每个可能的运行时条件(同样,我们不希望在之前没有错误时为我们的用户制造错误).
为什么我们仍然必须使用带引号的字符串文字来strict
在JS中打开?当然可以在这里使用一些更强烈"打字"的东西,例如调用内置函数,比如说Object.UseStrict()
类似的东西.必须诉诸字符串文字的原因是什么?
我正在尝试使用以下约定,我已被指示用于我的"Hello, World!"
程序的良好/正确/安全的Perl代码:
use strict;
use warnings;
Run Code Online (Sandbox Code Playgroud)
我在我的主Windows 7操作系统上使用(Strawberry)Perl 5.12创建并成功运行了以下"Hello World"程序:
!#/usr/bin/perl
use strict;
use warnings;
print "Hello, World!\n";
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,我得到的回报是"Hello, World!"
.
令我印象深刻的是,使用Perl 5.14在我的虚拟化Linux Mint 14操作系统上运行终端的同一程序产生了以下错误:
"use" not allowed in expression at /PATH/hello_world.pl line 2, at end of line
syntax error at /PATH/hello_world.pl line 2, near "use strict"
BEGIN not safe after errors--compilation aborted at /PATH/hello_world.pl line 3.
Run Code Online (Sandbox Code Playgroud)
我创建了其它的"Hello World"程序.随后,而不包含use strict;
或use warnings;
线条,并且还一个用-w
,我在一些教程已经看到,这表明,如果我没有记错的话,这将警告被打开.
我的两个备用版本都正常工作,因为它们产生了我预期的结果:
Hello, World!
Run Code Online (Sandbox Code Playgroud)
我不能确定的是,我是否需要use
从版本5.14及更高版本的Perl程序中的语句,或者如果-w
在第一行的末尾写入它就好了.
我想我可以在我的所有Perl程序中使用一致的头文件,无论是Windows还是Linux,Perl 5.12或5.14或其他.
一个简单的Javascript问题,例如我有一个像这样的Angular app.js;
'use strict';
var eventsApp = angular.module('eventsApp',[]);
Run Code Online (Sandbox Code Playgroud)
我读到在Javascript文件的开头使用"use strict"使得该文件中的所有变量都被处理为严格模式,这意味着当你使用全局变量(?)时会抛出错误,但是我们如何访问来自我们所有控制器和服务的"eventApp"对象,如果它不在全球范围内?
我正在使用YUI Compressor来缩小JS文件.我有这个代码:
(function(global) {
"use strict";
var X=1;
/*** my code here ***/
}(window));
Run Code Online (Sandbox Code Playgroud)
我用这个命令压缩它:
$> java -jar yuicompressor-2.4.7.jar test.js -o test.min.js --verbose
Run Code Online (Sandbox Code Playgroud)
压缩输出文件中不存在"use strict"提示.似乎YUI删除了所有提示.并输出此警告:
[WARNING] Invalid hint syntax: use strict
(function(global){ ---> "use strict" <--- ;var X=1;...
Run Code Online (Sandbox Code Playgroud)
虽然,我的代码在压缩后工作正常,但我想知道YUICompressor是否存在一个参数来保留提示和/或避免该警告的方法.
有什么建议?谢谢.
除了Chrome之外,此代码在所有浏览器中都会生效"ok"警告:
eval("var outer = 0; function test() {'use strict'; outer = 1; } test(); alert('ok');");
Run Code Online (Sandbox Code Playgroud)
我正在做的就是从'use strict'
函数引用外部变量,所有这些都在eval
上下文中.Chrome说
Uncaught ReferenceError: outer is not defined
Run Code Online (Sandbox Code Playgroud)
注意:我devtool: 'eval'
在Webpack中使用时最初遇到过它.
我在JavaScript中定义了以下函数:
function _snr(id) {
"use strict";
this.e = "something";
}
Run Code Online (Sandbox Code Playgroud)
我通过JSLint运行我的代码,它建议我在函数中添加"use strict".
当我e
现在做抛出和未定义的错误.从一些初步调查来看,似乎已经不再定义了this
过去引用的内容_snr
.
我读过"严格使用",并发现它用于防止不安全的做法.有人可以解释一下这是不安全的吗?什么是"use strict"实际上在做什么以及如何修复我的代码?
根据这个http://caniuse.com/use-strict 'use strict'
不支持IE 8/9版.
我的问题是,在IE 8/9或与其不兼容的浏览器中使用'use strict'是否真的安全?它会破坏我的代码吗?
我是javascript的新手,我正在尝试通过JSLint进行验证.我应该在哪里使用"use strict"在全球范围内使用它并验证?
这给了我错误"在语句位置使用严格'的意外表达式.":
"use strict";
console.log('doing js in head-section');
function helloWorld() {
console.log('called function helloWorld()');
alert('Hello World from a JS function showing an alert!');
}
function helloMyNumber() {
console.log('called function helloMyNumber()');
var max = 42;
var yourLuckyNumber = prompt('Enter your lucky number (between 1 and '+ max +')');
var myLuckyNumber = Math.floor(Math.random()*(max+1));
var paragraph = document.getElementById('luckynumber');
paragraph.innerHTML = paragraph.innerHTML + ' Your lucky number is: ' + yourLuckyNumber + '. Mine is: ' + myLuckyNumber + '. They ' + …
Run Code Online (Sandbox Code Playgroud) use-strict ×10
javascript ×9
jslint ×2
angularjs ×1
ecmascript-5 ×1
eval ×1
minify ×1
perl ×1
shebang ×1
strict ×1
v8 ×1