chore(i18n,curriculum): processed translations (#42581)
This commit is contained in:
@ -17,7 +17,7 @@ Animal.prototype.eat = function() {
|
||||
};
|
||||
```
|
||||
|
||||
在这一节以及下一节挑战中我们将学习如何在 `Bird` 和 `Dog` 中重用 `Animal's` 中的方法,而无需重新定义它们。 这里我们会用到构造函数的继承特性。 这一节挑战中我们学习第一步:创建一个超类 `supertype`(或者叫父类)的实例。 你已经学会了一种创建 `Animal` 实例的方法,即使用 `new` 操作符:
|
||||
在这一节以及下一节挑战中我们将学习如何在 `Bird` 和 `Dog` 中重用 `Animal` 中的方法,而无需重新定义它们。 这里我们会用到构造函数的继承特性。 这一节挑战中我们学习第一步:创建一个超类 `supertype`(或者叫父类)的实例。 你已经学会了一种创建 `Animal` 实例的方法,即使用 `new` 操作符:
|
||||
|
||||
```js
|
||||
let animal = new Animal();
|
||||
@ -29,7 +29,7 @@ let animal = new Animal();
|
||||
let animal = Object.create(Animal.prototype);
|
||||
```
|
||||
|
||||
`Object.create(obj)` 创建了一个新对象,并指定了 `obj` 作为新对象的 `prototype`。 回忆一下,我们之前说过 `prototype` 就像是创建对象的“配方”。 如果我们把 `animal` 的 `prototype` 设置为与 `Animal's` 构造函数的 `prototype` 一样,那么就相当于让 `animal` 这个实例的配方与 `Animal` 其他实例的配方一样了。
|
||||
`Object.create(obj)` 创建了一个新对象,并指定了 `obj` 作为新对象的 `prototype`。 回忆一下,我们之前说过 `prototype` 就像是创建对象的“配方”。 如果我们把 `animal` 的 `prototype` 设置为与 `Animal` 构造函数的 `prototype` 一样,那么就相当于让 `animal` 这个实例具有与 `Animal` 的其他实例相同的“配方”了。
|
||||
|
||||
```js
|
||||
animal.eat();
|
||||
|
@ -36,7 +36,7 @@ Bird.prototype.eat = function() {
|
||||
};
|
||||
```
|
||||
|
||||
如果你有一个实例:`let duck = new Bird();`,然后你调用了 `duck.eat()`,以下就是 JavaScript 在 `duck’s` 的 `prototype` 链上寻找方法的过程:
|
||||
如果你有一个实例:`let duck = new Bird();`,然后你调用了 `duck.eat()`,以下就是 JavaScript 在 `duck` 的 `prototype` 链上寻找方法的过程:
|
||||
|
||||
1. `duck` => `eat()` 是定义在这里吗? 不是。
|
||||
2. `Bird` => `eat()` 是定义在这里吗? => 是的。 执行它并停止往上搜索。
|
||||
|
@ -19,7 +19,7 @@ let duck = new Bird();
|
||||
duck.constructor
|
||||
```
|
||||
|
||||
但是 `duck` 和其他所有 `Bird` 的实例都应该表明它们是由 `Bird` 创建的,而不是由 `Animal` 创建的。 为此,你可以手动把 `Bird's` 的 constructor 属性设置为 `Bird` 对象:
|
||||
但是 `duck` 和其他所有 `Bird` 的实例都应该表明它们是由 `Bird` 创建的,而不是由 `Animal` 创建的。 为此,你可以手动将 `Bird` 的构造函数属性设置为 `Bird` 对象:
|
||||
|
||||
```js
|
||||
Bird.prototype.constructor = Bird;
|
||||
|
@ -12,7 +12,7 @@ dashedName: use-prototype-properties-to-reduce-duplicate-code
|
||||
|
||||
当只有两个实例时可能并不是什么问题,但想象一下如果有数百万个实例。 这将会产生许许多多重复的变量。
|
||||
|
||||
这里有一个更好的方法可以解决上述问题,那就是使用 `Bird’s` 的 `prototype`。 `prototype` 是一个可以在所有 `Bird` 实例之间共享的对象。 以下是一个在 `Bird prototype` 中添加 `numLegs` 属性的示例:
|
||||
更好的方法是使用 `Bird` 的 `prototype`。 `prototype` 是一个可以在所有 `Bird` 实例之间共享的对象。 以下是一个在 `Bird prototype` 中添加 `numLegs` 属性的示例:
|
||||
|
||||
```js
|
||||
Bird.prototype.numLegs = 2;
|
||||
@ -45,7 +45,7 @@ assert(beagle.numLegs !== undefined);
|
||||
assert(typeof beagle.numLegs === 'number');
|
||||
```
|
||||
|
||||
`numLegs` 应该是一个 `prototype` 属性而不是一个 `own` 属性。
|
||||
`numLegs` 应该是一个 `prototype` 属性,而不是一个自身属性。
|
||||
|
||||
```js
|
||||
assert(beagle.hasOwnProperty('numLegs') === false);
|
||||
|
Reference in New Issue
Block a user