ES5特性 - ECMAScript特性 - Javascript核心

原文: http://pij.robinqu.me/JavaScript_Core/ECMAScript/es5.htmljavascript

源代碼: https://github.com/RobinQu/Programing-In-Javascript/blob/master/chapters/JavaScript_Core/ECMAScript/es5.mdhtml

  • 本文須要補充更多例子
  • 本文存在批註,但該網站的Markdown編輯器不支持,因此沒法正常展現,請到原文參考。

ES5特性

本文將簡單列舉ES5的核心特性。ES5多半是擴展原生對象的功能,讓ObjectArrayFunction更增強大。其餘的特性包括strict mode和一下期待已久的工具方法(例如JSON.parse等)。java

ES5的大部分特性1都在主流瀏覽器(IE9+)中支持了。並且大部分特性,均可以經過Javascript墊片(pollyfill)在運行時環境實現2git

Object

全部對象操做中,若是o不是Object類型,將會拋出TypeError異常。github

Object.getPrototypeOf(o)3

獲取給丁對象的prototype對象。等價於之前的o.__proto__數組

Object.getOwnPropertyDescriptor(o,p)4

獲取對象描述。和Object.defineProperty的相關方法。瀏覽器

Object.getOwnPropertyNames(o)5

獲取自有屬性名列表。結果列表將不包含原型鏈上的屬性。編輯器

Object.create(o,p)6

以給丁對象oprototype建立新的對象並返回。若是對象描述p存在,就使用其定義剛建立的對象(相似調用Object.defineProperties(obj,p))。函數

Object.defineProperty(o,p,attrs)7

根據規則attrs定義對象o上,屬性名爲p的屬性工具

Object.defineProperties(o,props)8

根據對象描述props來定義對象o,一般props包含多個屬性的定義。

Object.seal(o)9

一個對象在默認狀態下,

  1. extensible: 能夠添加新的屬性
  2. configurable: 能夠修改已有屬性的特性

Object.seal會改變這兩個特性,既不能擴展新屬性,也不能修改已有屬性的特性。

Object.freeze(o)10

將對象的每一個自有自有屬性(own property)作以下操做:

  • 屬性的writable特性置爲false
  • 屬性的configurable特性置爲false

同時,該對象將不可擴展。可見,該方法比Object.seal更加嚴格的限制了對一個對象的將來改動。

Object.preventExtensions(o)11

將對象置爲不可擴展。

Object.isSealed(o)12

判斷一個對象是否sealed

  • 對象的每一個自有屬性:若是屬性的configurable特性爲true,則返回false
  • 若是對象爲extensible的,那麼返回false
  • 不知足以上兩個條件,則返回true

Object.isFrozen(o)13

  • 對每一個自有屬性,若是該屬性的configurablewritable特性爲true,則返回false
  • 若是對象爲extensible的,那麼返回false
  • 不知足以上兩個條件,則返回true

Object.isExtensible(o)14

判對一個對象是否可擴展。

Object.keys(o)15

返回對象o的全部可枚舉(enumerable)屬性的名稱。

Object.prototype.isPrototypeOf(v)16

檢查對象是不是位於給定對象v的原型鏈上。

Object.prototype.propertyIsEnumerable(p)

檢查一個對象上的屬性p是否可枚舉。

Array

Array.isArray(a)

判斷a是否爲爲真正的Array

Array.prototype.indexOf(e,i)17

使用「嚴格等」來判斷元素e在數組中的索引號。一個可選的搜索起點i

Array.prototype.lastIndexOf(e,i)18

獲取元素e在數組中最後出現的位置。起始位置i爲可選。

Array.prototype.every(t,c)

測試數組中的每一個元素都知足測試t。以後介紹的全部數組遍歷方法,都支持一個可選的上下文對象c,能夠靈活設置回調函數的執行上下文。傳遞給數組的測試函數、遍歷函數一般有以下簽名:

function(item, index, array) {}

Array.prototype.some(t,c)

測試數組中是否有元素知足測試t

Array.prototype.forEach(f,c)

使用函數f遍歷每一個數組的元素。

Array.prototype.map(f,c)

使用函數f修改每一個數組的每一個元素。按順序收集f的每一個返回值,並返回這個新組成的數組。

Array.prototype.filter(f,c)

收集經過函數測試f的書組元素。

Array.prototype.reduce(r,v)19

從左向右,使用函數r彙集數組的每一個元素。能夠可選的制定一個初始值v

Array.prototype.reduceRight(r,v)20

Array.prototype.reduce的從右向左的版本。

String

String.prototpye.trim

去掉字符串兩頭的空白符和換行符。

字符訂閱

//property access on strings
"abc"[2] === "b"

Function

Function.prototype.bind(thisTarget, arg1,...argn)21

爲了指定當前函數的上下文對象和運行參數,該函數建立一個新的函數,保留給定的this對象和運行參數。

JSON

JSON.parse(text)

根據rfc462722標準解析JSON文本。

JSON.stringify(obj)

將指定的對象obj序列化爲JSON文本。

Date

Date.now

獲取當前時間距1970.1.1 00:00:00的毫秒數。

Date.prototype.toISOString

根據ISO860123生成時間字符串。

(new Date).toISOString()
'2014-04-02T08:31:53.049Z'

其餘特性

  • 放開了關鍵字不容許做爲屬性名的限制24
  • getter和setter函數25

  1. http://kangax.github.io/es5-compat-table/ 

  2. https://github.com/es-shims/es5-shim 

  3. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/GetPrototypeOf 

  4. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor 

  5. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames 

  6. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create 

  7. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty 

  8. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperties 

  9. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/seal 

  10. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/freeze 

  11. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/preventExtensions 

  12. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/isSealed 

  13. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/isFrozen 

  14. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/isExtensible 

  15. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys 

  16. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/isPrototypeOf 

  17. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf 

  18. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf 

  19. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce 

  20. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduceRight 

  21. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind 

  22. http://www.ietf.org/rfc/rfc4627.txt 

  23. http://en.wikipedia.org/wiki/ISO_8601 

  24. http://stackoverflow.com/questions/8099270/use-of-reserved-words-in-javascript 

  25. http://ejohn.org/blog/javascript-getters-and-setters/ 

相關文章
相關標籤/搜索