use strict from jslint

本文介绍了JavaScript中的Strict Mode特性,这是ECMAScript 5引入的一种新功能,它可以在更严格的上下文中运行程序或函数,帮助开发者避免一些常见的编程错误,并禁用了一些易混淆的功能。
部署运行你感兴趣的模型镜像

Why do I get missing "use strict" statement error when I run my java script code through Crockford's JSLint

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions. Strict mode helps out in a couple ways:
  • It catches some common coding bloopers, throwing exceptions.
  • It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
  • It disables features that are confusing or poorly thought out.
Also note you can apply "strict mode" to the whole file... Or you can use it only for a specific function. Conclusion is it helps you make fewer errors, by detecting more things that could lead to breakages.

References:
  1. You can also look at article about that : John Resig - ECMAScript 5 Strict Mode, JSON, and More 
  2. You can try JSLint (The JavaScript Code Quality Tool) online by visiting:http://www.jslint.com/ 
  3. JSHint looks pretty interesting too: http://www.jshint.com/

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### JavaScript中 'use strict' 的使用 在JavaScript里,`'use strict'` 是一个指示代码以严格模式运行的指令。可以在全局作用域或函数作用域中使用该指令。 - **全局作用域使用**:将 `'use strict'` 放在脚本文件的顶部,那么整个脚本都会以严格模式运行。 ```javascript 'use strict'; // 以下代码都处于严格模式 const num = 10; ``` - **函数作用域使用**:把 `'use strict'` 放在函数内部,只有该函数内部的代码会以严格模式运行。 ```javascript function strictFunction() { 'use strict'; // 该函数内的代码处于严格模式 let variable = 'example'; } ``` ### 'use strict' 的作用 严格模式对JavaScript代码施加了更严格的语法和语义规则,主要作用如下: - **消除一些不合理、不严谨的语法**:在非严格模式下,给未声明的变量赋值会隐式创建全局变量,而严格模式会抛出错误。 ```javascript // 严格模式下会报错 'use strict'; x = 10; ``` - **增强安全性**:严格模式禁止使用一些可能导致安全漏洞的特性,例如 `with` 语句,在严格模式下使用会报错。 ```javascript 'use strict'; with (Math) { console.log(PI); // 严格模式下会报错 } ``` - **简化代码的调试**:严格模式下,一些静默失败的操作会抛出错误,让开发者能更早发现问题。例如,在严格模式下,对只读属性赋值会抛出错误。 ```javascript 'use strict'; const obj = {}; Object.defineProperty(obj, 'prop', { value: 10, writable: false }); obj.prop = 20; // 严格模式下会报错 ``` ### 'use strict' 与 `forEach` 的关联 在引用[1]中提到,`forEach` 的回调函数在严格模式下对 `this` 的处理与非严格模式有所不同。在非严格模式下,`forEach` 回调函数中的 `this` 默认指向全局对象(在浏览器中是 `window` 对象);而在严格模式下,如果没有明确指定 `this` 的值,`forEach` 回调函数中的 `this` 是 `undefined`。 ```javascript 'use strict'; const arr = ['a']; arr.forEach(function() { console.log(this); // 输出 "undefined" }); ``` 在非严格模式下,以下代码中的 `this` 会指向全局对象: ```javascript const arr = ['a']; arr.forEach(function() { console.log(this); // 在浏览器中输出 window 对象 }); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值