ECMA5-规则变化

1、

ECMAScript-262第5版,引入所谓的严格模式(strict mode)。开启严格模式的实现会禁用语言中的那些不稳定、不可靠和不安全的特性。据说出于安全方面的考虑,arguments.callee属性将在严格模式下被“封杀”。因此,在处于严格模式时,访问arguments.callee会导致TypeError(参见ECMA-262第5版的10.6节)。

严格模式下的各种限制规范:http://www.cnblogs.com/zhaodongyu/p/3253738.html

2、变量显示声明

在ES5严格模式下,未声明的变量会抛出错误。例如这样

//之间并未声明name变量
name = "haha"

3、eval

任何试图使用 “eval”都是好被阻止的,即使是把eval方法赋值给一个变量或者对象的属性。

// All generate errors...
obj.eval = ...
obj.foo = eval;
var eval = ...;
for ( var eval in ... ) {}
function eval(){}
function test(eval){}
function(eval){}
new Function("eval")
另外,试图通过eval引进新的变量也是被阻止的:

eval("var a = false;");
print( typeof a ); // undefined
4、function

试图覆盖arguments对象将会报错:

arguments = [...]; // not allowed
定义相同名字的 auguments 会报错:
function( foo, foo ) {}.
访问 arguments.caller 和 arguments.callee会抛出异常,因此引用任何匿名方法都需要名字,就想这样:
setTimeout(function later(){
  // do stuff...
  setTimeout( later, 1000 );
}, 1000 );
arguments和caller属性在其他方法内不再存在,并且重新定义他们的能力也是不允许的。
function test(){
  function inner(){
    // Don't exist, either
    test.arguments = ...; // Error
    inner.caller = ...; // Error
  }
}

最后,一个长期存在的bug被解决掉了:当 null 或者 undefined被塞进来就变成了 global对象。
strict模式下会阻止这样发生,会抛出错误。
(function(){ ... }).call( null ); // Exception
再例如在非strict模式下:
var yyp = "yyp";
(function(){ console.log(this.yyp) }).call( null);//输出 yyp

在strict模式下:

"use strict"
 var yyp = "yyp";
(function(){ console.log(this.yyp) }).call( null );//TypeError: this is null
5、with(){}

with表达式在严格模式下彻底不能用。例如:

"use strict"
with(this){} //SyntaxError: strict mode code may not contain 'with' statements

6、JSON

本地支持 JSON 对象。
JSON.parse:将JSON 字符串转换成js对象
JSON:stringify: 将js对象转换成 json字符串

7、bind()

内置 .bind方法来强制设定一个方法的执行上下文(context)

Function.prototype.bind(thisArg, arg1, arg2....)
8、Date

支持解析和输出 iso-formatted 日期

var date = new Date("2009-05-21T16:06:05.000Z");
 
// Prints 2009-05-21T16:06:05.000Z
print( date.toISOString() );

9、trim()

支持截取空字符串

10、Array

增加了好多方法例如:indexOf, lastIndexOf, every, some, forEach, map, filter, reduce, and reduceRight.

isArray的实现:

Array.isArray = function( array ) {
  return Object.prototype.toString.call( array ) === "[object Array]";
};


参考:

http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值