ES 5 新增特性汇总

请期待下一篇《ES 6 新增特性汇总》和下下一篇《ES 2016 新增特性汇总》。
最近有同学问我 ES 5 到底新增了哪些语法,我一时还真想不全。于是就想去找找看有没有这样的文档,然而却发现并没有。因为 Ecma International 直接公布了一份完整的 ES 5.1 的规格文档,并没有特别列出 ES 5 和 ES 3 的区别。

最后找到一份看起来挺全的列表,于是就想翻译一下备忘。

- Trailing commas are ok
多余的逗号不报错,比如 {a:1,b:2,}

- No reserved words for property names
属性名可以使用关键字和保留字了,比如 { if:1, else:2 }

- NaN, Infinity, undefined : are all constants
NaN、Infinity、undefined 都是常量了,不可更改。

- parseInt() defaults to radix 10
parseInt 第二个参数默认为 10(真好)

- /regexp/ produces new reg ex object every time
正则字面量每次都会产生一个新的对象

- JSON.parse(), JSON.stringify()
添加 JSON 序列化和反序列化

- Function.prototype.bind
函数新增 bind 方法(贺老说 bind 写在后面有点不好用)

- String.prototype.trim
字符串终于自带头尾去空格方法了,如 ' abc '.trim() === 'abc'

- Array.prototype.every, filter, forEach, indexOf, lastIndexOf, map, reduce, reduceRight, some
数组添加了一系列方法,终于能脱离 Underscore.js 了

- Date.now()
Date.now 现在才加进来的?可怕。

- Date.prototype.toISOString
日期新增 toISOString 方法

- new Date(string) and Date.parse(string) will try ISO format 1st
新增对 ISO 日期格式的支持

- Array.isArray()
判断一个对象是不是数组……以前苦了大家了

- Object.keys(), Object.create(), Object.defineProperty, Object.defineProperties, 
Object.getOwnPropertyDescriptor(), Object.getOwnPropertyNames(obj), Object.getPrototypeOf(obj)
- Object.seal(), Object.freeze(), Object.preventExtensions(), Object.isSealed(), Object.isFrozen(), 
Object.isExtensible()
对象新增一系列方法,使得在 JS 中模拟 Java 变得更丝滑。我最喜欢的还是 Object.create()

- Property attributes: writeable, value, enumerable, configurable, get, set
对象的属性可以添加各种配置了

- 'use strict';
- Strict Mode: 
  No more implied global variables within functions.
  this is not bound to the global object by function form.
  apply and call do not default to the global object.
  No with statement.
  Setting a writeable: false property will throw.
  Deleting a configurable: false property will throw.
  Restrictions on eval.
  eval and arguments are reserved.
  arguments not linked to parameters.
  No more arguments.caller or arguments.callee.
  No more octal literals.
  Duplicate names in an object literal or function parameters are a syntax error

严格模式
函数里没有隐式的全局变量了,你要创建全局变量必须是显式的。比如想用 a = 1 创建全局变量是不行的。
this 不会默认指向全局对象(比如 window 或者 global)了。
call 和 apply 也不会默认使用全局对象了。
不准用 with
如果一个属性的 writeable 是 false,那么你给这个属性赋值就会报错。
如果一个属性的 configurable 是 false,那么你 delete 这个属性就会报错。
对 eval 和 arguments 做出了限制。以下代码每行都会报错:

  eval = 17;
  arguments++;
  ++eval;
  var obj = { set p(arguments) { } };
  var eval;
  try { } catch (arguments) { }
  function x(eval) { }
  function arguments() { }
  var y = function eval() { };
  var f = new Function("arguments", "'use strict'; return 17;");
arguments 只保存原始参数。对形参的赋值不会对 arguments 有影响。
不准用 arguments.caller 和 arguments.callee
不支持八进制字面量,比如 var a = 015 会报错。
对象字面量或者函数形参中,如果有重复的名字,就会报错。

Strict Mode 更详细的参考,见 MDN

作者:方应杭

35 thoughts on “ES 5 新增特性汇总”

  1. Fantastic beat ! I would like to apprentice at the same time as you amend your website, how could i subscribe for a weblog site? The account aided me a appropriate deal. I have been a little bit familiar of this your broadcast offered brilliant clear idea|

  2. Spot on with this write-up, I actually believe that this amazing site needs much more attention. I’ll probably be back again to read more, thanks for the info!|

  3. Thank you for every other magnificent article. The place else may anyone get that kind of information in such a perfect means of writing? I have a presentation next week, and I’m at the search for such info.|

  4. I have been exploring for a little for any high quality articles or blog posts on this kind of area . Exploring in Yahoo I at last stumbled upon this site. Reading this info So i am satisfied to show that I have an incredibly excellent uncanny feeling I found out exactly what I needed. I most unquestionably will make certain to don?t fail to remember this web site and give it a look on a constant basis.|

  5. What i don’t understood is actually how you’re not really much more neatly-liked than you may be now. You’re very intelligent. You understand thus significantly on the subject of this matter, made me in my view believe it from so many various angles. Its like men and women are not interested except it is something to accomplish with Woman gaga! Your individual stuffs excellent. All the time take care of it up!|

  6. Please let me know if you’re looking for a writer for your blog. You have some really great posts and I believe I would be a good asset. If you ever want to take some of the load off, I’d love to write some material for your blog in exchange for a link back to mine. Please send me an e-mail if interested. Cheers!|

  7. Hi there to all, how is the whole thing, I think every one is getting more from this web site, and your views are good designed for new viewers.|

  8. Hi there! This is kind of off topic but I need some guidance from an established blog. Is it tough to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about creating my own but I’m not sure where to start. Do you have any tips or suggestions? Thank you|

  9. Hey there would you mind letting me know which hosting company you’re using? I’ve loaded your blog in 3 completely different web browsers and I must say this blog loads a lot faster then most. Can you recommend a good web hosting provider at a honest price? Cheers, I appreciate it!|

  10. It’s the best time to make a few plans for the longer term and it is time to be happy. I’ve read this submit and if I could I want to recommend you some fascinating issues or advice. Maybe you could write subsequent articles relating to this article. I want to read more things approximately it!|

  11. Hmm is anyone else having problems with the images on this blog loading? I’m trying to find out if its a problem on my end or if it’s the blog. Any responses would be greatly appreciated.|

  12. Thanks a bunch for sharing this with all people you really recognise what you’re speaking approximately! Bookmarked. Kindly additionally seek advice from my site =). We could have a hyperlink alternate arrangement among us|

  13. This is very interesting, You are an excessively skilled blogger. I have joined your rss feed and look forward to seeking extra of your excellent post. Additionally, I’ve shared your website in my social networks|

  14. hello there and thank you for your information – I’ve certainly picked up something new from right here. I did however expertise some technical points using this website, as I experienced to reload the site lots of times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your quality score if advertising and marketing with Adwords. Well I am adding this RSS to my e-mail and could look out for much more of your respective intriguing content. Make sure you update this again soon.|

  15. Good day! I could have sworn I’ve visited your blog before but after going through many of the posts I realized it’s new to me. Anyhow, I’m definitely delighted I came across it and I’ll be book-marking it and checking back frequently!|

  16. An impressive share! I’ve just forwarded this onto a co-worker who was conducting a little research on this. And he in fact ordered me dinner due to the fact that I discovered it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanks for spending some time to talk about this matter here on your blog.|

  17. I do not even know the way I stopped up right here, but I believed this put up used to be good. I don’t recognise who you might be however certainly you are going to a well-known blogger if you are not already. Cheers!|

发表评论

电子邮件地址不会被公开。