菜单

从ECMAScript规范解读this

2019年4月16日 - 皇家前端

JavaScript 深远之从 ECMAScript 规范解读 this

2017/05/17 · JavaScript
· this

原来的小说出处: 冴羽   

以前都是依照函数的行使状态来掌握this,分为八种情形:

最初的文章出处

JavaScript深刻之从ECMAScript规范解读this
ECMA-262-3 in detail. Chapter 3.
This
ECMAScript5.1中文版
ECMAScript5.1英文版

大家好,小编是IT修真院温哥华分院第06期学员,1枚正直善良的web程序员。

前言

在《JavaScript深远之实行上下文栈》中讲到,当JavaScript代码实施一段可进行代码(executable
code)时,会创制对应的实施上下文(execution context)。

对于每一个实行上下文,都有四个十分重要性质

后天首要讲讲this,不过不佳讲。

……

因为我们要从ECMASciript伍规范起来讲起。

先奉上ECMAScript 五.一业外市点:

英文版:

中文版:

让我们开始驾驭规范吧!

Types


首先是第 8 章 Types:

Types are further subclassified into ECMAScript language types and
specification types.

An ECMAScript language type corresponds to values that are directly
manipulated by an ECMAScript programmer using the ECMAScript language.
The ECMAScript language types are Undefined, Null, Boolean, String,
Number, and Object.

A specification type corresponds to meta-values that are used within
algorithms to describe the semantics of ECMAScript language constructs
and ECMAScript language types. The specification types are Reference,
List, Completion, Property Descriptor, Property Identifier, Lexical
Environment, and Environment Record.

作者们差不多的翻译一下:

ECMAScript 的项目分为语言类型和正规类型。

ECMAScript 语言类型是开发者直接运用 ECMAScript
能够操作的。其实正是大家常说的Undefined, Null, Boolean, String, Number,
和 Object。

而行业内部类型约等于 meta-values,是用来用算法描述 ECMAScript 语言结构和
ECMAScript 语言类型的。规范类型包涵:Reference, List, Completion,
Property Descriptor, Property Identifier, Lexical Environment, 和
Environment Record。

没懂?无妨,大家若是知道在 ECMAScript
规范中还有①种只存在王丽萍规中的类型,它们的机能是用来描述语言底层行为逻辑

先天给我们分享一下,修真院官方网站JS任务中大概会使用到的知识点:

澳门皇家赌场,Types

率先是第10章Types:

Types are further subclassified into ECMAScript language types and
specification types.

An ECMAScript language type corresponds to values that are directly
manipulated by an ECMAScript programmer using the ECMAScript language.
The ECMAScript language types are Undefined, Null, Boolean, String,
Number, and Object.

A specification type corresponds to meta-values that are used within
algorithms to describe the semantics of ECMAScript language constructs
and ECMAScript language types. The specification types are Reference,
List, Completion, Property Descriptor, Property Identifier, Lexical
Environment, and Environment Record.

大家大致的翻译一下:

ECMAScript的项目分为语言类型和行业内部类型。

ECMAScript语言类型是开拓者直接行使ECMAScript可以操作的。其实正是大家常说的Undefined,
Null, Boolean, String, Number, 和 Object。

而行业内部类型相当于meta-values,是用来用算法描述ECMAScript语言结构和ECMAScript语言类型的。规范类型包蕴:Reference,
List, Completion, Property Descriptor, Property Identifier, Lexical
Environment, 和 Environment Record。

没懂?不要紧,我们重点看当中的Reference类型。

叩问规范

英文版:http://es5.github.io/\#x15.1
中文版:http://yanhaijing.com/es5/\#115

Reference


这什么又是 Reference ?

让大家看 8.7 章 The Reference Specification Type

The Reference type is used to explain the behaviour of such operators
as delete, typeof, and the assignment operators.

故而 Reference 类型便是用来讲明诸如 delete、typeof
以及赋值等操作行为的。

抄袭尤雨溪大大的话,就是:

那里的 Reference 是3个 Specification Type,也正是“只设有于专业里的肤浅类型”。它们是为着越来越好地描述语言的底层行为逻辑才存在的,但并不存在于实际的
js 代码中

再看接下去的那段具体介绍 Reference 的内容:

A Reference is a resolved name binding.

A Reference consists of three components, the base value, the
referenced name and the Boolean valued strict reference flag.

The base value is either undefined, an Object, a Boolean, a String, a
Number, or an environment record (10.2.1).

A base value of undefined indicates that the reference could not be
resolved to a binding. The referenced name is a String.

这段讲述了 Reference 的组成,由四个组成都部队分,分别是:

  1. base value: 正是性质所在的靶子也许就是EnvironmentRecord,它的值只大概是 undefined, an Object, a Boolean, a
    String。

  2. referenced name: 正是性质的名目。

  3. strict reference: 是或不是处在严俊情势。

举个例证:

var foo = 1;

// 对应的Reference是:
var fooReference = {
    base: EnvironmentRecord,
    name: 'foo',
    strict: false
};

再举个例证:

var foo = {
    bar: function () {
        return this;
    }
};

foo.bar(); // foo

// bar对应的Reference是:
var BarReference = {
    base: foo,
    propertyName: 'bar',
    strict: false
};

还要规范中还提供了赚取 Reference 组成都部队分的方式,比如 GetBase 和
IsPropertyReference

那五个艺术很简短,轻巧看壹看:

1.GetBase

GetBase(V). Returns the base value component of the reference V.

返回 reference 的 base value

  1. IsPropertyReference

IsPropertyReference(V). Returns true if either the base value is an
object or HasPrimitiveBase(V) is true; otherwise returns false.

简轻易单的精晓:要是 base value 是一个指标,就回去true

简述Js中this的指向


Reference

那什么又是Reference?

让我们看八.七章 The Reference Specification Type:

The Reference type is used to explain the behaviour of such operators
as delete, typeof, and the assignment operators.

由此Reference类型正是用来说明诸如delete、typeof以及赋值等操作行为的。

抄袭尤雨溪大大的话,正是:

那边的 Reference 是一个 Specification Type,也等于“只存在于专业里的抽象类型”。它们是为着更加好地描述语言的底层行为逻辑才存在的,但并不设有于实际的
js 代码中。

再看接下去的那段具体介绍Reference的始末:

A Reference is a resolved name binding.

A Reference consists of three components, the base value, the
referenced name and the Boolean valued strict reference flag.

The base value is either undefined, an Object, a Boolean, a String, a
Number, or an environment record (10.2.1).

A base value of undefined indicates that the reference could not be
resolved to a binding. The referenced name is a String.

那段讲了Reference有多少个组成都部队分,分别是:

与此同时base value是undefined, an Object, a Boolean, a String, a Number, or
an environment record个中的1种

reference name是字符串。

然而那几个到底是怎么吧?

让大家简要的明白base
value是性质所在的靶子或然正是EnvironmentRecord,referenced
name正是性质的称号

哦,举个例子:

var foo = 1; var fooReference = { base: EnvironmentRecord, name: ‘foo’,
strict: false };

1
2
3
4
5
6
7
var foo = 1;
 
var fooReference = {
  base: EnvironmentRecord,
  name: ‘foo’,
  strict: false
};

再举个例证:

var foo = { bar: function () { return this; } }; foo.bar(); // foo var
fooBarReference = { base: foo, propertyName: ‘bar’, strict: false };

1
2
3
4
5
6
7
8
9
10
11
12
13
var foo = {
  bar: function () {
    return this;
  }
};
foo.bar(); // foo
 
var fooBarReference = {
  base: foo,
  propertyName: ‘bar’,
  strict: false
};

再便是规范中还提供了能够收获Reference组成都部队分的方式,比如 GetBase 和
IsPropertyReference

那四个艺术很粗大略,轻巧看1看:

1.GetBase

GetBase(V). Returns the base value component of the reference V.

返回reference的base value

2.IsPropertyReference

IsPropertyReference(V). Returns true if either the base value is an
object or HasPrimitiveBase(V) is true; otherwise returns false.

简言之的明亮:base value是object,就重回true

1、Types

Types are further subclassified into ECMAScript language types and
specification types.

An ECMAScript language type corresponds to values that are directly
manipulated by an ECMAScript programmer using the ECMAScript language.
The ECMAScript language types are Undefined, Null, Boolean, String,
Number, and Object.

A specification type corresponds to meta-values that are used within
algorithms to describe the semantics of ECMAScript language constructs
and ECMAScript language types. The specification types are Reference,
List, Completion, Property Descriptor, Property Identifier, Lexical
Environment, and Environment Record.

翻译过来正是:

花色又再分为ECMAScript语言类型和行业内部类型。

ECMAScript语言类型是开采者使用ECMAScript直接操作的类型。ECMAScript语言类型是Undefined,Null,Boolean,String,
Number, 和Object。

规范类型约等于meta-values,用来用算法来叙述ECMAScript
语言结构和言语类型的。规范类型是:Reference,List,Completion,Property
Descriptor,Property Identifier, Lexical Environment, and Environment
Record。

咱俩需求知道在 ECMAScript
规范中还有一种只设有于专业中的类型,它们的功能是用来描述语言底层行为逻辑。

GetValue


除此而外,紧接着在八.⑦.1 章规范中就讲了2个用于从 Reference
类型获取对应值的章程: GetValue

粗略模拟 GetValue 的接纳:

var foo = 1;

var fooReference = {
    base: EnvironmentRecord,
    name: 'foo',
    strict: false
};

GetValue(fooReference) // 1;

GetValue 再次来到对象属性真正的值,可是要留意:
调用 GetValue,重临的将是现实的值,而不再是三个 Reference

一.背景介绍

当JavaScript代码实施一段可举行代码(executable
code)时,会创设对应的实践上下文(execution context)。
对于每一个实行上下文,都有多少个第3性质:


相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图