4.2.2 The Strict Variant of ECMAScript ECMAScript的嚴格版本
- 詳細內容
- 分類:ECMA-262 第五版
- 發佈:2013-04-02, 週二 22:59
- 點擊數:1650
4.2.2 The Strict Variant of ECMAScript
The ECMAScript Language recognises the possibility that some users of the language may wish to restrict their usage of some features available in the language. They might do so in the interests of security, to avoid what they consider to be error-prone features, to get enhanced error checking, or for other reasons of their choosing. In support of this possibility, ECMAScript defines a strict variant of the language. The strict variant of the language excludes some specific syntactic and semantic features of the regular ECMAScript language and modifies the detailed semantics of some features. The strict variant also specifies additional error conditions that must be reported by throwing error exceptions in situations that are not specified as errors by the non-strict form of the language.
4.2.2 ECMAScript的嚴格版本
ECMAScript 語言承認一個可能性,某些語言的使用者會希望限制語言中的一些特徵的用途。這樣做的目的可能是為了安全性,或是避免他門認為容易產生錯誤的特徵,強化錯誤檢查,或是其他選擇的理由。為了這個可能性,ECMAScript為這語言定義了一個嚴格的版本。此語言的嚴格版本包含了一些特殊的語法與語意特徵,這些特徵都是正規的ECMAScript語言,並修改了這些特徵語意的細節。嚴格版本也指明了額外的錯誤狀態,這些錯誤狀態必須藉由丟出例外來回報,這些情況在語言的非嚴格類型中,並不會指明為錯誤。
The strict variant of ECMAScript is commonly referred to as the strict mode of the language. Strict mode selection and use of the strict mode syntax and semantics of ECMAScript is explicitly made at the level of individual ECMAScript code units. Because strict mode is selected at the level of a syntactic code unit, strict mode only imposes restrictions that have local effect within such a code unit. Strict mode does not restrict or modify any aspect of the ECMAScript semantics that must operate consistently across multiple code units. A complete ECMAScript program may be composed for both strict mode and non-strict mode ECMAScript code units. In this case, strict mode only applies when actually executing code that is defined within a strict mode code unit.
In order to conform to this specification, an ECMAScript implementation must implement both the full unrestricted ECMAScript language and the strict mode variant of the ECMAScript language as defined by this specification. In addition, an implementation must support the combination of unrestricted and strict mode code units into a single composite program.
ECMAScript的嚴格版本通常被稱之為是這個語言的嚴格模式。ECMAScript嚴格模式語法與語義的選擇與使用是明確的形成在個別的程式碼區塊層級內。因為嚴格模式是在語法程式碼層級下進行選擇的,因此嚴格模式的限制只在這樣的程式碼單元內有區域性的影響。嚴格模式並不限制或修該任何ECMAScript需要一貫的操作多個程式碼單元的語義的觀點,。一個完整的ECMAScript程式可以式由嚴格模式與非嚴格模式所組合而成。這種情況下,嚴格模式只在真正執行到定義在嚴格模式程式碼單元內的程式碼才有作用。
為了遵守這份規範書,ECMAScript介面必須要實現完整的非嚴格模式的ECMAScript語言,以及此份規範書所定義的ECMAScript 語言的嚴格模式版本。介面必須支援非嚴格模式與嚴格模式的組合同時存在在一個單一的組合程式。
翻譯註解:
嚴格模式可以與非嚴格模式同時存在在程式中,譬如說可以在某個函式內使用嚴格模式,而其他部分則不使用嚴格模式。
要使用嚴格模式其實很簡單字串"use strict"。譬如說:
(function (){
"use strict";
............
})();
如此在function 內就是使用嚴格模式。
按個讚!~支持本站!~
FB推薦載入中