|
506 | 506 | "id": "587d7daf367417b2b2512b7d",
|
507 | 507 | "title": "Iterate Over All Properties",
|
508 | 508 | "description": [
|
509 |
| - "You have now seen two kinds of properties: <code>own</code> properties and <code>prototype</code> properties. <code>Own</code> properties are defined directly on the object instance itself. And <code>prototype</code> properties are defined on the <code>prototype</code>.", |
510 |
| - "<blockquote>function Bird(name) {<br> this.name = name; //own property<br>}<br><br>Bird.prototype.numLegs = 2; // prototype property<br><br>let duck = new Bird(\"Donald\");</blockquote>", |
511 |
| - "Here is how you add <code>duck’s</code> <code>own</code> properties to the array <code>ownProps</code> and <code>prototype</code> properties to the array <code>prototypeProps</code>:", |
512 |
| - "<blockquote>let ownProps = [];<br>let prototypeProps = [];<br><br>for (let property in duck) {<br> if(duck.hasOwnProperty(property)) {<br> ownProps.push(property);<br> } else {<br> prototypeProps.push(property);<br> }<br>}<br><br>console.log(ownProps); // prints [\"name\"]<br>console.log(prototypeProps); // prints [\"numLegs\"]</blockquote>", |
| 509 | + "现在你已经了解了两种属性: <code>自身</code> 属性和 <code>原型</code> 属性。 <code>自身</code> 属性是直接在对象实例它自己内部定义的。而 <code>原型</code> 属性是定义在 <code>prototype</code> 上的:", |
| 510 | + "<blockquote>function Bird(name) {<br> this.name = name; // 自身属性 <br>}<br><br>Bird.prototype.numLegs = 2; // 原型属性 <br><br>let duck = new Bird(\"Donald\");</blockquote>", |
| 511 | + "这是一个告诉你如何将 <code>duck’s</code> <code>自身</code> 属性和 <code>原型</code> 属性分别添加到 <code>ownProps</code> 数组和 <code>prototypeProps</code> 数组里面:", |
| 512 | + "<blockquote>let ownProps = [];<br>let prototypeProps = [];<br><br>for (let property in duck) {<br> if(duck.hasOwnProperty(property)) {<br> ownProps.push(property);<br> } else {<br> prototypeProps.push(property);<br> }<br>}<br><br>console.log(ownProps); // 输出 [\"name\"]<br>console.log(prototypeProps); // 输出 [\"numLegs\"]</blockquote>", |
513 | 513 | "<hr>",
|
514 |
| - "Add all of the <code>own</code> properties of <code>beagle</code> to the array <code>ownProps</code>. Add all of the <code>prototype</code> properties of <code>Dog</code> to the array <code>prototypeProps</code>." |
| 514 | + "将 <code>beagle</code> 的所有属性都添加到 code>ownProps</code> 数组里面去。将 <code>Dog</code> 的所有 <code>原型</code> 属性添加到 <code>prototypeProps</code> 数组中。" |
515 | 515 | ],
|
516 | 516 | "tests": [
|
517 | 517 | {
|
518 |
| - "text": "The <code>ownProps</code> array should include <code>\"name\"</code>.", |
519 |
| - "testString": "assert(ownProps.indexOf('name') !== -1, 'The <code>ownProps</code> array should include <code>\"name\"</code>.');" |
| 518 | + "text": "这个 <code>ownProps</code> 数组应该包含了 <code>\"name\"</code> 这个值。", |
| 519 | + "testString": "assert(ownProps.indexOf('name') !== -1, '这个 <code>ownProps</code> 属性应该包含了 <code>\"name\"</code> 这个值。');" |
520 | 520 | },
|
521 | 521 | {
|
522 |
| - "text": "The <code>prototypeProps</code> array should include <code>\"numLegs\"</code>.", |
523 |
| - "testString": "assert(prototypeProps.indexOf('numLegs') !== -1, 'The <code>prototypeProps</code> array should include <code>\"numLegs\"</code>.');" |
| 522 | + "text": "这个 <code>prototypeProps</code> 数组应该包含了 <code>\"numLegs\"</code> 这个值。", |
| 523 | + "testString": "assert(prototypeProps.indexOf('numLegs') !== -1, '这个 <code>prototypeProps</code> 数组应该包含了 <code>\"numLegs\"</code> 这个值。');" |
524 | 524 | },
|
525 | 525 | {
|
526 |
| - "text": "Solve this challenge without using the built in method <code>Object.keys()</code>.", |
527 |
| - "testString": "assert(!/\\Object.keys/.test(code), 'Solve this challenge without using the built in method <code>Object.keys()</code>.');" |
| 526 | + "text": "在不使用内置方法 <code>Object.keys()</code> 的情况下解决这个挑战。", |
| 527 | + "testString": "assert(!/\\Object.keys/.test(code), '在不使用内置方法 <code>Object.keys()</code> 的情况下解决这个挑战。');" |
528 | 528 | }
|
529 | 529 | ],
|
530 | 530 | "solutions": [
|
|
550 | 550 | "let ownProps = [];",
|
551 | 551 | "let prototypeProps = [];",
|
552 | 552 | "",
|
553 |
| - "// Add your code below this line ", |
| 553 | + "// 在下一行添加你的代码 ", |
554 | 554 | "",
|
555 | 555 | "",
|
556 | 556 | ""
|
|
564 | 564 | "id": "587d7daf367417b2b2512b7e",
|
565 | 565 | "title": "Understand the Constructor Property",
|
566 | 566 | "description": [
|
567 |
| - "There is a special <code>constructor</code> property located on the object instances <code>duck</code> and <code>beagle</code> that were created in the previous challenges:", |
568 |
| - "<blockquote>let duck = new Bird();<br>let beagle = new Dog();<br><br>console.log(duck.constructor === Bird); //prints true<br>console.log(beagle.constructor === Dog); //prints true</blockquote>", |
569 |
| - "Note that the <code>constructor</code> property is a reference to the constructor function that created the instance.", |
570 |
| - "The advantage of the <code>constructor</code> property is that it's possible to check for this property to find out what kind of object it is. Here's an example of how this could be used:", |
| 567 | + "在上一个挑战中创建的实例对象 <code>duck</code> 和 <code>beagle</code> 都有一个特殊的 <code>constructor</code> 属性:", |
| 568 | + "<blockquote>let duck = new Bird();<br>let beagle = new Dog();<br><br>console.log(duck.constructor === Bird); //输出 true<br>console.log(beagle.constructor === Dog); //输出 true</blockquote>", |
| 569 | + "需要注意到的是这个 <code>constructor</code> 属性是对创建这个实例的构造函数的一个引用。", |
| 570 | + "<code>constructor</code> 属性的一个优势就是通过检查这个属性来找出它是一个什么样的对象。下面是一个例子,来看看是怎么使用的:", |
571 | 571 | "<blockquote>function joinBirdFraternity(candidate) {<br> if (candidate.constructor === Bird) {<br> return true;<br> } else {<br> return false;<br> }<br>}</blockquote>",
|
572 |
| - "<strong>Note</strong><br>Since the <code>constructor</code> property can be overwritten (which will be covered in the next two challenges) it’s generally better to use the <code>instanceof</code> method to check the type of an object.", |
| 572 | + "<strong>注意:</strong><br>由于 <code>constructor</code> 属性可以被重写(在下面两节挑战中将会遇到) 所以通常使用 <code>instanceof</code> 方法来检查对象的类型要更好。", |
573 | 573 | "<hr>",
|
574 |
| - "Write a <code>joinDogFraternity</code> function that takes a <code>candidate</code> parameter and, using the <code>constructor</code> property, return <code>true</code> if the candidate is a <code>Dog</code>, otherwise return <code>false</code>." |
| 574 | + "写一个 <code>joinDogFraternity</code> 函数,传入一个 <code>candidate</code> 参数并使用 <code>constructor</code> 属性来判断传入的 candidate 是不是 <code>Dog</code> 创建的对象实例,如果是返回 <code>true</code> 否则返回 <code>false</code>。" |
575 | 575 | ],
|
576 | 576 | "tests": [
|
577 | 577 | {
|
578 |
| - "text": "<code>joinDogFraternity</code> should be defined as a function.", |
579 |
| - "testString": "assert(typeof(joinDogFraternity) === 'function', '<code>joinDogFraternity</code> should be defined as a function.');" |
| 578 | + "text": "<code>joinDogFraternity</code> 应该被定义为一个函数。", |
| 579 | + "testString": "assert(typeof(joinDogFraternity) === 'function', '<code>joinDogFraternity</code> 应该被定义为一个函数。');" |
580 | 580 | },
|
581 | 581 | {
|
582 |
| - "text": "<code>joinDogFraternity</code> should return true if<code>candidate</code> is an instance of <code>Dog</code>.", |
583 |
| - "testString": "assert(joinDogFraternity(new Dog(\"\")) === true, '<code>joinDogFraternity</code> should return true if<code>candidate</code> is an instance of <code>Dog</code>.');" |
| 582 | + "text": "如果 <code>candidate</code> 是 <code>Dog</code> 的一个对象实例,则 <code>joinDogFraternity</code> 函数应该返回 <code>true</code>。", |
| 583 | + "testString": "assert(joinDogFraternity(new Dog(\"\")) === true, '如果 <code>candidate</code> 是 <code>Dog</code> 的一个对象实例,则 <code>joinDogFraternity</code> 函数应该返回 <code>true</code>。');" |
584 | 584 | },
|
585 | 585 | {
|
586 |
| - "text": "<code>joinDogFraternity</code> should use the <code>constructor</code> property.", |
587 |
| - "testString": "assert(/\\.constructor/.test(code) && !/instanceof/.test(code), '<code>joinDogFraternity</code> should use the <code>constructor</code> property.');" |
| 586 | + "text": "<code>joinDogFraternity</code> 应该要使用 <code>constructor</code> 属性。", |
| 587 | + "testString": "assert(/\\.constructor/.test(code) && !/instanceof/.test(code), '<code>joinDogFraternity</code> 应该要使用 <code>constructor</code> 属性。');" |
588 | 588 | }
|
589 | 589 | ],
|
590 | 590 | "solutions": [
|
|
603 | 603 | " this.name = name;",
|
604 | 604 | "}",
|
605 | 605 | "",
|
606 |
| - "// Add your code below this line", |
| 606 | + "// 在下一行添加你的代码", |
607 | 607 | "function joinDogFraternity(candidate) {",
|
608 | 608 | " ",
|
609 | 609 | "}",
|
|
618 | 618 | "id": "587d7daf367417b2b2512b7f",
|
619 | 619 | "title": "Change the Prototype to a New Object",
|
620 | 620 | "description": [
|
621 |
| - "Up until now you have been adding properties to the <code>prototype</code> individually:", |
| 621 | + "到目前为止,你已经可以单独给 <code>prototype</code> 添加属性了:", |
622 | 622 | "<blockquote>Bird.prototype.numLegs = 2;</blockquote>",
|
623 |
| - "This becomes tedious after more than a few properties.", |
| 623 | + "这将在添加许多属性的时候变得单调乏味的。", |
624 | 624 | "<blockquote>Bird.prototype.eat = function() {<br> console.log(\"nom nom nom\");<br>}<br><br>Bird.prototype.describe = function() {<br> console.log(\"My name is \" + this.name);<br>}</blockquote>",
|
625 |
| - "A more efficient way is to set the <code>prototype</code> to a new object that already contains the properties. This way, the properties are added all at once:", |
| 625 | + "一种更有效的方法就是给对象的 <code>prototype</code> 设置为一个已经包含了属性的新对象。这样一来,所有属性都可以一次性被添加进来:", |
626 | 626 | "<blockquote>Bird.prototype = {<br> numLegs: 2, <br> eat: function() {<br> console.log(\"nom nom nom\");<br> },<br> describe: function() {<br> console.log(\"My name is \" + this.name);<br> }<br>};</blockquote>",
|
627 | 627 | "<hr>",
|
628 |
| - "Add the property <code>numLegs</code> and the two methods <code>eat()</code> and <code>describe()</code> to the <code>prototype</code> of <code>Dog</code> by setting the <code>prototype</code> to a new object." |
| 628 | + "通过给 <code>prototype</code> 设置为新对象的方法,给 <code>Dog</code> 构造函数的 <code>原型</code> 上添加一个属性 <code>numLegs</code> 和两个方法 <code>eat()</code> 、<code>describe()</code>。" |
629 | 629 | ],
|
630 | 630 | "tests": [
|
631 | 631 | {
|
632 |
| - "text": "<code>Dog.prototype</code> should be set to a new object.", |
633 |
| - "testString": "assert((/Dog\\.prototype\\s*?=\\s*?{/).test(code), '<code>Dog.prototype</code> should be set to a new object.');" |
| 632 | + "text": "<code>Dog.prototype</code> 应该被设置为一个新对象。", |
| 633 | + "testString": "assert((/Dog\\.prototype\\s*?=\\s*?{/).test(code), '<code>Dog.prototype</code> 应该被设置为一个新对象。');" |
634 | 634 | },
|
635 | 635 | {
|
636 |
| - "text": "<code>Dog.prototype</code> should have the property <code>numLegs</code>.", |
637 |
| - "testString": "assert(Dog.prototype.numLegs !== undefined, '<code>Dog.prototype</code> should have the property <code>numLegs</code>.');" |
| 636 | + "text": "<code>Dog.prototype</code> 应该拥有属性 <code>numLegs</code>。", |
| 637 | + "testString": "assert(Dog.prototype.numLegs !== undefined, '<code>Dog.prototype</code> 应该拥有属性 <code>numLegs</code>。');" |
638 | 638 | },
|
639 | 639 | {
|
640 |
| - "text": "<code>Dog.prototype</code> should have the method <code>eat()</code>.", |
641 |
| - "testString": "assert(typeof Dog.prototype.eat === 'function', '<code>Dog.prototype</code> should have the method <code>eat()</code>.'); " |
| 640 | + "text": "<code>Dog.prototype</code> 应该拥有方法 <code>eat()</code>。", |
| 641 | + "testString": "assert(typeof Dog.prototype.eat === 'function', '<code>Dog.prototype</code> 应该拥有方法 <code>eat()</code>。'); " |
642 | 642 | },
|
643 | 643 | {
|
644 |
| - "text": "<code>Dog.prototype</code> should have the method <code>describe()</code>.", |
645 |
| - "testString": "assert(typeof Dog.prototype.describe === 'function', '<code>Dog.prototype</code> should have the method <code>describe()</code>.'); " |
| 644 | + "text": "<code>Dog.prototype</code> 应该拥有方法 <code>describe()</code>。", |
| 645 | + "testString": "assert(typeof Dog.prototype.describe === 'function', '<code>Dog.prototype</code> 应该拥有方法 <code>describe()</code>。'); " |
646 | 646 | }
|
647 | 647 | ],
|
648 | 648 | "solutions": [
|
|
662 | 662 | "}",
|
663 | 663 | "",
|
664 | 664 | "Dog.prototype = {",
|
665 |
| - " // Add your code below this line", |
| 665 | + " // 在下一行添加你的代码", |
666 | 666 | " ",
|
667 | 667 | "};"
|
668 | 668 | ],
|
|
0 commit comments