Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

可以使用 `Object.keys` 列出所有可枚举键值,然后输出
可以使用 `Object.keys` 获取所有可枚举的键,并输出其列表

为了使 `toString` 不可枚举,我们使用属性描述器来定义它。`Object.create` 语法允许我们为一个对象提供属性描述器作为第二参数。
为了使 `toString` 不可枚举,我们使用一个属性描述器来定义它。`Object.create` 语法允许我们为一个对象提供属性描述器作为第二参数。

```js run
*!*
let dictionary = Object.create(null, {
 toString: { // 定义 toString 方法
   value() { // value 是一个函数
toString: { // 定义 toString 属性
value() { // value 是一个 function
return Object.keys(this).join();
}
}
Expand All @@ -17,13 +17,15 @@ let dictionary = Object.create(null, {
dictionary.apple = "Apple";
dictionary.__proto__ = "test";

// apple 和 __proto__ 在循环内
// apple 和 __proto__ 在循环中
for(let key in dictionary) {
 alert(key); // "apple",然后 "__proto__"
alert(key); // "apple",然后是 "__proto__"
}

// 通过 toString 得到逗号分隔的属性值
alert(dictionary.toString()); // "apple,__proto__"
// 通过 toString 处理获得的以逗号分隔的属性列表
alert(dictionary); // "apple,__proto__"
```

当我们使用描述器创建一个属性,它的标识默认是 `false`。因此在以上代码中,`dictonary.toString` 是不可枚举的。
当我们使用描述器创建一个属性,它的标识默认是 `false`。因此在上面这段代码中,`dictonary.toString` 是不可枚举的。

请阅读 [](info:property-descriptors) 一章进行回顾。
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ importance: 5

---

# 给字典对象添加 toString 方法
# 为 dictionary 添加 toString 方法

有一个对象 `dictionary`,通过 `Object.create(null)` 创建,用来存储任意键值对
这儿有一个通过 `Object.create(null)` 创建的,用来存储任意 `key/value` 对的对象 `dictionary`

为该对象添加方法 `dictionary.toString()`,返回所有键的列表,用逗号隔开。你的 `toString` 方法不能对该对象使用 `for...in`。
为该对象添加 `dictionary.toString()` 方法,该方法应该返回以逗号分隔的所有键的列表。你的 `toString` 方法不应该在使用 `for...in` 循环遍历数组的时候显现出来

以下是它的运行例子
它的工作方式如下

```js
let dictionary = Object.create(null);

*!*
// 添加 dictionary.toString 方法的代码
// 你的添加 dictionary.toString 方法的代码
*/!*

// 添加一些数据
dictionary.apple = "Apple";
dictionary.__proto__ = "test"; // __proto__ 在这里是正常参数
dictionary.__proto__ = "test"; // 这里 __proto__ 是一个常规的属性键

// 只有 apple 和 __proto__ 在循环内
// 在循环中只有 apple 和 __proto__
for(let key in dictionary) {
 alert(key); // "apple",然后 "__proto__"
alert(key); // "apple", then "__proto__"
}

// your toString in action
// 你的 toString 方法在发挥作用
alert(dictionary); // "apple,__proto__"
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

第一个调用中 `this == rabbit`,其他的 `this` 等同于 `Rabbit.prototype`,因为它是逗号之前的对象
第一个调用中 `this == rabbit`,其他的 `this` 等同于 `Rabbit.prototype`,因为 `this` 就点符号前面的对象

因此只有第一个调用显示 `Rabbit`,其他的都是 `undefined`:
所以,只有第一个调用显示 `Rabbit`,其他的都显示的是 `undefined`:

```js run
function Rabbit(name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Rabbit.prototype.sayHi = function() {
let rabbit = new Rabbit("Rabbit");
```

以下调用得到的结果是否相同
以下调用做的是相同的事儿还是不同的

```js
rabbit.sayHi();
Expand Down
Loading