You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**To make `user.hi()`calls work, JavaScript uses a trick -- the dot `'.'`returns not a function, but a value of the special [Reference Type](https://tc39.github.io/ecma262/#sec-reference-specification-type).**
The Reference Type is a "specification type". We can't explicitly use it, but it is used internally by the language.
75
+
Reference Type 是 ECMA 中的一个“规范类型”。我们不能直接使用它,但它被用在 JavaScript 语言内部。
76
76
77
-
The value of Reference Type is a three-value combination `(base, name, strict)`, where:
77
+
Reference Type 的值是一个三个值的组合 `(base, name, strict)`,其中:
78
78
79
-
-`base`is the object.
80
-
-`name`is the property name.
81
-
-`strict`is true if `use strict`is in effect.
79
+
-`base`是对象。
80
+
-`name`是属性名。
81
+
-`strict`在 `use strict`模式下为 true。
82
82
83
-
The result of a property access `user.hi`is not a function, but a value of Reference Type. For`user.hi` in strict mode it is:
83
+
对属性 `user.hi`访问的结果不是一个函数,而是一个 Reference Type 的值。对于`user.hi`,在严格模式下是:
84
84
85
85
```js
86
-
// Reference Type value
86
+
// Reference Type 的值
87
87
(user, "hi", true)
88
88
```
89
89
90
-
When parentheses `()`are called on the Reference Type, they receive the full information about the object and its method, and can set the right `this` (`=user` in this case).
90
+
当 `()`被在 Reference Type 上调用时,它们会接收到关于对象和对象的方法的完整信息,然后可以设置正确的 `this`(在此处 `=user`)。
91
91
92
-
Reference type is a special "intermediary" internal type, with the purpose to pass information from dot `.`to calling parentheses `()`.
92
+
Reference Type 是一个特殊的“中间人”内部类型,目的是从 `.`传递信息给 `()` 调用。
93
93
94
-
Any other operation like assignment `hi = user.hi`discards the reference type as a whole, takes the value of `user.hi` (a function) and passes it on. So any further operation "loses" `this`.
94
+
任何例如赋值 `hi = user.hi`等其他的操作,都会将 Reference Type 作为一个整体丢弃掉,而会取 `user.hi`(一个函数)的值并继续传递。所以任何后续操作都“丢失”了 `this`。
95
95
96
-
So, as the result, the value of `this`is only passed the right way if the function is called directly using a dot `obj.method()`or square brackets `obj['method']()`syntax (they do the same here). Later in this tutorial, we will learn various ways to solve this problem such as [func.bind()](/bind#solution-2-bind).
Reference Type is an internal type of the language.
100
+
Reference Type 是语言内部的一个类型。
101
101
102
-
Reading a property, such as with dot `.` in `obj.method()`returns not exactly the property value, but a special "reference type" value that stores both the property value and the object it was taken from.
That's for the subsequent method call `()`to get the object and set `this`to it.
104
+
这是为了随后的方法调用 `()`获取来源对象,然后将 `this`设为它。
105
105
106
-
For all other operations, the reference type automatically becomes the property value (a function in our case).
106
+
对于所有其它操作,Reference Type 会自动变成属性的值(在我们这个情况下是一个函数)。
107
107
108
-
The whole mechanics is hidden from our eyes. It only matters in subtle cases, such as when a method is obtained dynamically from the object, using an expression.
109
-
110
-
111
-
112
-
113
-
114
-
result of dot `.` isn't actually a method, but a value of `` needs a way to pass the information about `obj`
0 commit comments