文章中一些名词的翻译存疑,没有查过正式的中文名称

前面都是具体过程的解释,懒得看可以直接看获取思路

有关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的子类

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

ECMAScript中关于如何获取this的定义的更多相关文章

  1. 实际上ECMAScript中并没有对类的定义

    首先,我们用一个经典例子来简单阐述一下ECMAScript中的继承机制. 在几何学上,实质上几何形状只有两种,即椭圆形(是圆形的)和多边形(具有一定数量的边).圆是椭圆的一种,它只有一个焦点.三角形. ...

  2. Linux中的两个经典宏定义:获取结构体成员地址,根据成员地址获得结构体地址;Linux中双向链表的经典实现。

    倘若你查看过Linux Kernel的源码,那么你对 offsetof 和 container_of 这两个宏应该不陌生.这两个宏最初是极客写出的,后来在Linux内核中被推广使用. 1. offse ...

  3. C#开发微信门户及应用(14)-在微信菜单中采用重定向获取用户数据

    我曾经在系列文章中的<C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍>中介绍了微信菜单里面的重定向操作,通过这个重定向操作,我们可以获取一个code值,然后获取用户的open ...

  4. asp.net三层架构 及其中使用泛型获取实体数据介绍

    asp.net中使用泛型获取实体数据可以发挥更高的效率,代码简洁方便,本例采用三层架构.首先在model层中定义StuInfo实体,然后在 DAL层的SQLHelper数据操作类中定义list< ...

  5. Android中如何利用attrs和styles定义控件

    一直有个问题就是,Android中是如何通过布局文件,就能实现控件效果的不同呢?比如在布局文件中,我设置了一个TextView,给它设置了 textColor,它就能够改变这个TextView的文本的 ...

  6. 在C#中调用API获取网络信息和流量

    原文 在C#中调用API获取网络信息和流量 最近一项目中要求显示网络流量,而且必须使用C#. 事实上,调用 IpHlpApi.dll 的 GetIfTable API 可以轻易获得网络信息和网络流量. ...

  7. Spring拦截器中通过request获取到该请求对应Controller中的method对象

    背景:项目使用Spring 3.1.0.RELEASE,从dao到Controller层全部是基于注解配置.我的需求是想在自定义的Spring拦截器中通过request获取到该请求对应于Control ...

  8. SSIS中Sql Task 获取系统变量

    原文:SSIS中Sql Task 获取系统变量 执行 SQL 任务使用不同的连接类型时,SQL 命令的语法使用不同的参数标记.例如,ADO.NET 连接管理器类型要求 SQL 命令使用格式为 @var ...

  9. Oracle中使用游标获取指定数据表的所有字段名对应的字符串

    操作步骤:打开PLSQL Developer后,直接执行下面的语句就可以出来 --Oracle中使用游标获取指定数据表的所有字段名对应的字符串 declare mytablename VARCHAR( ...

随机推荐

  1. 如何学习FPGA?FPGA学习必备的基础知识

    如何学习FPGA?FPGA学习必备的基础知识 时间:2013-08-12 来源:eepw 作者: 关键字:FPGA   基础知识       FPGA已成为现今的技术热点之一,无论学生还是工程师都希望 ...

  2. Win7 Object_Header之TypeIndex解析

    在暴力搜索内存进程对象反隐藏进程这篇文章中,我们提到: Object Header偏移0×008处Type成员为对象类型值,相同类型的对象具有相同的值.  自Window  7开始, _OBJECT_ ...

  3. 如何离线下载Chrome的安装包

    打开Chrome官网(自行搜索)点击下载后下载的是联网安装包,这对部分上网不方便的用户造成了一定的麻烦. http://www.google.cn/chrome/browser/desktop/ind ...

  4. 【项目经验】EasyUI Tree

    ITOO5.0开始了,我参加了伟大的基础系统,从整体上来说,基础系统有三个职能: 1.自己的核心职能--选课(公共选修课,专业选修课),课表: 2.为其他系统提供真实数据: 3.维护信息 而近两三天, ...

  5. SQL..如何用命令删除数据库中所有的表?

    要删除所有的用户表: declare @sql varchar(8000) SELECT @sql='drop table ' + name FROM sysobjects WHERE (type = ...

  6. Practical JAVA(二)关于对象的类型和equals函数

    Practice5,6,9,10,11,12,13,14,15 ==判断等号两边两个变量储存的值是否相同,如果是两个对象,则判断两个变量储存的对象地址是否相同. 大多数时候,我们需要判断的不是左右两个 ...

  7. hdu 1203 概率+01背包

    I NEED A OFFER! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  8. 用padding与margin做多个元素的等间距分布

    这样做的好处是不管有多少个元素等间距分布,都可以直接写在li中,而且由于是给a设定的样式,所以在字数不一致的情况下,样式仍然是统一的. html: <!DOCTYPE html> < ...

  9. hdu1106 排序水题

    Problem Description 输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整 ...

  10. hdu 2553 N皇后问题

    回溯. 一个主对角线,副对角线的技巧 //vis[0][i]表示第i列有没有皇后 vis[1][cur+i]表示副对角线 vis[2][cur-i+n]表示主对角线 #include <cstd ...