Skip to content

Commit 64dc587

Browse files
author
Carol
committed
translate 10-13 part
1 parent 2f803ea commit 64dc587

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

02-javascript-algorithms-and-data-structures/object-oriented-programming.json

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -506,25 +506,25 @@
506506
"id": "587d7daf367417b2b2512b7d",
507507
"title": "Iterate Over All Properties",
508508
"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>&nbsp;&nbsp;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>&nbsp;&nbsp;if(duck.hasOwnProperty(property)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;ownProps.push(property);<br>&nbsp;&nbsp;} else {<br>&nbsp;&nbsp;&nbsp;&nbsp;prototypeProps.push(property);<br>&nbsp;&nbsp;}<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>&nbsp;&nbsp;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>&nbsp;&nbsp;if(duck.hasOwnProperty(property)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;ownProps.push(property);<br>&nbsp;&nbsp;} else {<br>&nbsp;&nbsp;&nbsp;&nbsp;prototypeProps.push(property);<br>&nbsp;&nbsp;}<br>}<br><br>console.log(ownProps); // 输出 [\"name\"]<br>console.log(prototypeProps); // 输出 [\"numLegs\"]</blockquote>",
513513
"<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> 数组中。"
515515
],
516516
"tests": [
517517
{
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> 这个值。');"
520520
},
521521
{
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> 这个值。');"
524524
},
525525
{
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> 的情况下解决这个挑战。');"
528528
}
529529
],
530530
"solutions": [
@@ -550,7 +550,7 @@
550550
"let ownProps = [];",
551551
"let prototypeProps = [];",
552552
"",
553-
"// Add your code below this line ",
553+
"// 在下一行添加你的代码 ",
554554
"",
555555
"",
556556
""
@@ -564,27 +564,27 @@
564564
"id": "587d7daf367417b2b2512b7e",
565565
"title": "Understand the Constructor Property",
566566
"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> 属性的一个优势就是通过检查这个属性来找出它是一个什么样的对象。下面是一个例子,来看看是怎么使用的:",
571571
"<blockquote>function joinBirdFraternity(candidate) {<br>&nbsp;&nbsp;if (candidate.constructor === Bird) {<br>&nbsp;&nbsp;&nbsp;&nbsp;return true;<br>&nbsp;&nbsp;} else {<br>&nbsp;&nbsp;&nbsp;&nbsp;return false;<br>&nbsp;&nbsp;}<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> 方法来检查对象的类型要更好。",
573573
"<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>"
575575
],
576576
"tests": [
577577
{
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> 应该被定义为一个函数。');"
580580
},
581581
{
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>');"
584584
},
585585
{
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> 属性。');"
588588
}
589589
],
590590
"solutions": [
@@ -603,7 +603,7 @@
603603
" this.name = name;",
604604
"}",
605605
"",
606-
"// Add your code below this line",
606+
"// 在下一行添加你的代码",
607607
"function joinDogFraternity(candidate) {",
608608
" ",
609609
"}",
@@ -618,31 +618,31 @@
618618
"id": "587d7daf367417b2b2512b7f",
619619
"title": "Change the Prototype to a New Object",
620620
"description": [
621-
"Up until now you have been adding properties to the <code>prototype</code> individually:",
621+
"到目前为止,你已经可以单独给 <code>prototype</code> 添加属性了:",
622622
"<blockquote>Bird.prototype.numLegs = 2;</blockquote>",
623-
"This becomes tedious after more than a few properties.",
623+
"这将在添加许多属性的时候变得单调乏味的。",
624624
"<blockquote>Bird.prototype.eat = function() {<br>&nbsp;&nbsp;console.log(\"nom nom nom\");<br>}<br><br>Bird.prototype.describe = function() {<br>&nbsp;&nbsp;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> 设置为一个已经包含了属性的新对象。这样一来,所有属性都可以一次性被添加进来:",
626626
"<blockquote>Bird.prototype = {<br>&nbsp;&nbsp;numLegs: 2, <br>&nbsp;&nbsp;eat: function() {<br>&nbsp;&nbsp;&nbsp;&nbsp;console.log(\"nom nom nom\");<br>&nbsp;&nbsp;},<br>&nbsp;&nbsp;describe: function() {<br>&nbsp;&nbsp;&nbsp;&nbsp;console.log(\"My name is \" + this.name);<br>&nbsp;&nbsp;}<br>};</blockquote>",
627627
"<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>"
629629
],
630630
"tests": [
631631
{
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> 应该被设置为一个新对象。');"
634634
},
635635
{
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>');"
638638
},
639639
{
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>'); "
642642
},
643643
{
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>'); "
646646
}
647647
],
648648
"solutions": [
@@ -662,7 +662,7 @@
662662
"}",
663663
"",
664664
"Dog.prototype = {",
665-
" // Add your code below this line",
665+
" // 在下一行添加你的代码",
666666
" ",
667667
"};"
668668
],

0 commit comments

Comments
 (0)