ES5.0严格模式

 

1、启用es5    “use strict”

 1.1 如果在script顶部写启动 use strict  整个js都会启动es5

// 启动es5.0严格模式
'use strict'

// es5不支持arguments.callee、caller

function test(){
	console.log(arguments.callee);
}
test();

1.2 如果只对局部的代码启动es5.0,那么就在局部启动es5 

function demo(){
	console.log(arguments.callee);
}
demo();//正常执行

function test(){
	'use strict'
	console.log(arguments.callee);
}
test();//报错  不支持callee

1.3 es5所禁止的 with方法

// with:与命名空间搭配使用  可以更改作用域链。
// 缺点:如果作用域链多层,会导致效率低
// es5已禁止with方法
// with用法
var work = {
	part1 : {
		person1 : {
			name : '张三',
			age : 12
		},
		preson2 : {
			name : '李四',
			age : 13
		}
	},
	part2 : {

	}
};
// 当张三想要访问自己的命名空间时,可以使用with。
with(work.part1.person1){
	console.log(name);
	console.log(age);
}


1.4 变量声明赋值  需要var声明


	// 在es5模式下变量赋值,必须要有var声明
	b = 2;//报错
	var a = b = 2;//报错 b未定义

	var a = 2,
		b = 3;//支持这样声明

1.5 es5模式下的this  局部需要赋值this 否则为undefined

'use strict'

	console.log(this);//全局this依旧指向window
	function test(){
		console.log(this);//局部this如果没有调用者或者没有赋值就为undefined
	}
	test();

1.6 es5的func.call()改变this指向

	function demo(){
		console.log(this);
	}
	// 在es3的模式下,func.call(123);改变this指向,里面如果传原始值,会自动转成包装类
	// 在es5的模式下,func.call(123);里面传什么就是什么
	demo.call(123);//es3 :Number {123}
				   //es5 :123
	

1.7 es5 重复属性重复参数

	var obj = {
		name : name,
		name : name
	};
	// es5拒绝重复的属性,但不报错
	
	function fun(a,a){
		console.log(a);
	}
	fun(1);
	// es3: 重复的参数不报错  es5:重复参数报错

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值