js strict mode 'use strict'

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>严格模式</title>
	</head>
	<body>
		<script>
		// 00-严格模式 :解决js版本一些不合适 不合理的地方 IE10以上版本才会执行
		'use strict'; //为整个脚本开启严格模式 IE10以上版本
		// (function(){
		// 	// 'use strict';//为整个脚本开启严格模式
		// })()
		// function fn3(){
		// 	'use strict'; //为fn3这个函数开启严格模式
		// }
		// 01-严格模式的变化
		// 1一定要先声明再定义  
		// name='lhm'; //错误
		let name='lhm'; //正确
		//2不能随意删除声明的变量
		// delete name;//不可以
		//3 函数中!!的this不再指向window 而是undefined
		console.log(this);//window
		function fn(){
			console.log(this); //undefined
		}
		fn();
		//4构造眼熟必须加new 创建对象 不然里面的this指向的是undefined
		function Father(name,age){
		console.log(this);//undefined;
		this.name=name;
		this.age=age;
		}
		// Father("张学友",63);
		// console.log(Father.name);//报错
		let father=new Father("张学友",63);
		console.log(father.name);
		//05定时器还是指向还是window
		setTimeout(function(){
			console.log(this);//window
		},2000)
		//06函数参数不可以重名
		function fn2(a,a){
			console.log(a+a);
		} //报错
		
		//07 不可以在非函数代码块声明函数
		// if(true){
		// 	function fn4(){
		// 		console.log(" 不可以在非函数代码块声明函数")
		// 	}
		// } //报错
		</script>
	</body>
</html>

Guess you like

Origin blog.csdn.net/enhenglhm/article/details/123901267