ECMAScript中關於如何獲取this的定義

文章中一些名詞的翻譯存疑,沒有查過正式的中文名稱

前面都是具體過程的解釋,懶得看可以直接看獲取思路

有關this的取值請移步JavaScript筆記——this的取值

獲取this的過程

Runtime Semantics: Evaluation

  1. Return ResolveThisBinding();

ResolveThisBinding()

The abstract operation ResolveThisBinding determines the binding of the keyword this using the LexicalEnvironment of the running execution context. ResolveThisBinding performs the following steps:

抽象操作ResolveThisBinding通過running execution context中的LexicalEnvironment(詞法環境?)來決定關鍵字this的綁定,執行以下兩個步驟:

  1. Let envRec be GetThisEnvironment(); //獲取當前環境
  2. Return envRec.GetThisBinding(); //返回當前環境記錄中this的綁定

GetThisEnvironment()

The abstract operation GetThisEnvironment finds the Environment Record that currently supplies the binding of the keyword this. GetThisEnvironment performs the following steps:

抽象操作GetThisEnvironment尋找當前提供關鍵字this綁定的Environment Record(環境記錄?),執行以下步驟:

  1. Let lex be the running execution context’s LexicalEnvironment.
  2. Repeat
    a. Let envRec be lex’s EnvironmentRecord. //獲取當前環境記錄
    b. Let exists be envRec.HasThisBinding(). //判斷當前環境記錄中是否建立了this綁定
    c. If exists is true, return envRec. //是,則返回當前環境記錄
    d. Let outer be the value of lex’s outer environment reference. //否,則定義outer爲outer Lexical Environment
    e. Let lex be outer. //lex = outer,繼續循環

步驟2的循環總是會終止,因爲在environments列表中總是以擁有this的綁定的the global environment結尾

一些方法及Environment、Context的解釋

GetThisBinding()

Return the value of this Environment Record’s this binding. Throws a ReferenceError if the this binding has not been initialized.

返回Environment Record的this的綁定,如果未初始化綁定則拋出ReferenceError異常

HasThisBinding()

Determine if an Environment Record establishes a this binding. Return true if it does and false if it does not.

決定一個Environment Record是否建立了this綁定,是返回true,否則返回false

Lexical Environments

A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code. A Lexical Environment consists of an Environment Record and a possibly null reference to an outer Lexical Environment.

詞法環境(Lexical Environment)是用於定義具體變量和函數標識符的關聯,基於ECMAScript代碼的詞法嵌套結構的規範類型。
詞法環境包括詞法記錄(Environment Record)和對外層詞法環境(outer Lexical Environment)的引用,其引用可能爲空。

Lexical Environment包括幾種類型:

  • global environment
  • module environment
  • function environment

Lexical Environments和Environment Record的值僅僅是規範機制,無需對任何具體ECMAScript實現的人工程序作出響應,因此不能直接訪問或操作這些值。

Environment Records

規範中有兩種基本的Environment Record值,declarative Environment Records和object Environment Records

出於規範目的,可以將Environment Record看做一個抽象類,有三個具體的子類declarative Environment Record, object Environment Record, 和global Environment Record;
Function Environment Records和module Environment Records是declarative Environment Record的子類

Environment

running execution context

An execution context is a specification device that is used to track the runtime evaluation of code by an ECMAScript implementation. At any point in time, there is at most one execution context that is actually executing code. This is known as the running execution context.

執行上下文(execution context)是一種用於跟蹤ECMAScript實施代碼運行時評估的規範設備。
在任意時刻,至多有一個執行上下文在實際執行代碼,這就是running execution context。

獲取思路

Created with Raphaël 2.1.0開始ResolveThisBinding()envRec = GetThisEnvironment()return envRec.GetThisBinding()結束

GetThisEnvironment()的流程:

Created with Raphaël 2.1.0開始lex = running execution context’s LexicalEnvironmentenvRec = lex's EnvironmentRecordexists = envRec.HasThisBinding()exists == true?return envRec結束outer = outer Lexical Environmentlex = outeryesno

參考

ECMAScript® 2015 Language Specification

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章