use strict 的用法
来源:互联网 发布:软件我的手机 编辑:程序博客网 时间:2024/06/05 03:06
"use strict"是JavaScript中一个非常好的特性,而且非常容易使用。
如果你仅想在一个函数中使用:
这段代码会抛出一个错误因为 eyeLeft 出现了两次。这比你用眼睛去找错误要快多了。
未声明变量
重复的参数
限制函数中的arguments
使用方法
// file.js"use strict"function doStuff(){ // use strict is enabled here!}这样的file.js都会应用上"use strict"模式。
如果你仅想在一个函数中使用:
// file.jsfunction a(){ "use strict"; // use strict is enabled in this context function nestedFunction(){ // and here too }}
好处
检查对象中的重复键var zombie = { eyeLeft : 0, eyeRight: 1, // ... a lot of keys ... eyeLeft : 1}
这段代码会抛出一个错误因为 eyeLeft 出现了两次。这比你用眼睛去找错误要快多了。
未声明变量
plane = 5;你现在已经知道忘记在这个变量前面加var了。不过如果你不知道,调试起来是非常痛苦的,因为这个变量是声明在全局上下文(global context)中的,而且可能被其他地方改掉。想象一下,如果你声明了一个全局的 i, 在嵌套循环中可能会引起混乱。
重复的参数
function run(fromWhom, fromWhom){}注意fromWho出现了两次,因此会抛出一个错误。
限制函数中的arguments
var run = function(fromWhom){ arguments[0] = 'alien'; alert(fromWhom);}run('zombie');// alert: 'alien';现在你可以使用"use strict"
var run = function(fromWhom){ "use strict"; arguments[0] = 'alien'; alert(fromWhom);}run('zombie');// alert: 'zombie';arguments[0] = 'alien' 改变了参数fromWhom,use strict 又节约了你的时间。
阅读全文
0 0
- use strict 的用法
- Perl 关于 use strict 的用法
- Perl 关于 use strict 的用法
- javascript中的“use strict”的用法及好处
- use strict
- use strict
- "use strict";
- "use strict";
- 了解ECMA5.0的use strict
- 'use strict' - Javascript 的严格模式
- 关于javascript "use strict"的学习
- CNPM 遇到use strict的问题
- JavaScript的严格模式 use strict
- javascript 的"use strict"严格模式
- 使用"use strict"指令的目的
- Use strict and warning
- use strict from jslint
- use strict 介绍
- 时间戳转化为日期格式
- C++数据类型总结
- 设置模式(2)——模板方法模式
- python之datetime
- vim精讲
- use strict 的用法
- java通过itext生成PDF,设置单元格cell的最大高度 以及 itext7初尝
- git 初始化操作
- Java并发编程:Lock
- 递归的理解
- OpenCV之通道和位深的理解(CV_8UC1,CV_8SC1,CV_32FC1)
- 环境搭建(jdk+tomcat+maven+svn)
- JAVASE jar打开后闪退 ---Win7
- Uncaught TypeError: window.showModalDialog is not a function 谷歌