[Javascript]ECMAScript 5 已推出
InfoQ:ECMAScript 5 released,這篇來自InfoQ的文章中指出,ECMAScript 5已經推出了,這個版本終於將Javascript的鬆散結構做了一些改善,強制規定某些程式的寫法,真的算是一件好事阿,以下列舉他提到的幾個點,感覺起來確實有將過去很惹人厭,造成不好維護,不好偵錯的一些問題做了改善:
- Variables must be declared before use. In other words,
i=3
becomes a runtime error;var i=3
is needed (assumingi
is not in scope at the time) - Eval becomes a reserved word, and introducing new variables through eval cannot occur, so
eval("var i=3"); print(i);
will now throw an error. - Octal literals are no longer used; so
010
is ten, and not eight delete
cannot be used against arguments, functions or variables or other properties with the configurable flag set to falsewith
statements, often a source of errors, are no longer used and considered syntax errors- Functions can no longer have duplicate arguments with the same name
- Objects can no longer have duplicate properties with the same name
- The
arguments
andcaller
variables become immutable - Access to the global object becomes a runtime error
另外也提供了許多基本函式,第一項就是前陣子大家在討論的日期format,也加入了物件列舉功能,希望他的加強不只這些吧:
- Date now supports the ability to generate ISO8601 formatted dates (such as
20091209T12:34:56Z
) as well as to parse them - String now has a built-in
trim()
method - A new
JSON
object withparse
andstringify
to support efficient generation of JSON data; likeeval
but without the security implications of being able to reduce code. In addition, anyJSONValue
can be used, not justJSONObject
andJSONArray
as specified in RFC 4627. (4627 definesJSON-Text
to be constrained to an object or array.) - A new
bind
builtin has been added, with the same semantics as Prototype's bind() - Array now has standard functions, such as
indexOf()
,map()
,filter()
, andreduce()
- Object now has
seal()
(which prevents new properties being added or existing properties deleted) andfreeze()
(which makes all properties read-only, as well as preventing new properties being added or deleted) Object.keys()
lists all the enumerable properties of the objectObject.getOwnPropertyNames()
lists all the enumerable and non-enumerable propertiesObject.getPrototypeof()
returns the prototype of the given object
ECMAScript 5原則上應該還可以期待,目前只希望它的向下相容性是高的,避免我們之前寫的code要全面改版,這就很痛苦了。
參考資料:
ECMAScript 5 Objects and Properties
InfoQ:ECMAScript 5 released
EcmaScript 5: Changes to JavaScript
游舒帆 (gipi) 探索原力Co-founder,曾任TutorABC協理與鼎新電腦總監,並曾獲選兩屆微軟最有價值專家 ( MVP ),離開職場後創辦探索原力,致力於協助青少年培養面對未來的能力。認為教育與組織育才其實息息相關,都是在為未來儲備能量,2018年起成立為期一年的專題課程《職涯躍升的關鍵24堂課》,為培養台灣未來的領袖而努力。 |