八一八严格模式

19 篇文章 0 订阅

参考大佬地址

严格模式的作用

  1. 消除Javascript语法的一些不合理、不严谨之处,减少一些怪异行为;

  2. 消除代码运行的一些不安全之处,保证代码运行的安全;

  3. 提高编译器效率,增加运行速度;

  4. 为未来新版本的Javascript做好铺垫。

经过测试 IE6,7,8,9 均不支持严格模式

严格模式的使用限制 (es6下)

只要函数参数使用了默认值、解构赋值、或者扩展运算符,那么函数内部就不能显式设定为严格模式

	//参数默认值
	function testFn(a, b = 1) {
	    'use strict';
	    // Illegal 'use strict' directive in function with non-simple parameter list 
	    console.log(1)
	}
	// 这里的 non-simple parameter 指的就是形参有默认值了
	
	//扩展运算符
	function testFn1(...value) {
	    'use strict';
	    //Illegal 'use strict' directive in function with non-simple parameter list 
	    console.log(1)
	}
	
	//解构
	function testFn({ a, b }) {
	    'use strict';
	}

两种方法可以规避这种限制。
第一种:设定全局性的严格模式,这是合法的。

	'use strict';
	function testFn({ a, b } = {}) {
		// 因为严格模式下这种八进制写法是会报错的
		// Octal literals are not allowed in strict mode
	    console.log(013)
	}
	testFn()

第二种:j将函数包裹在一个无参数的立即执行函数里面

	const doSomething = (function() {
	    'use strict';
	    return function(value = 42) {
	        return value;
	    };
	}());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值