fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@@ -0,0 +1,40 @@
---
title: Add Methods After Inheritance
localeTitle: 继承后添加方法
---
## 继承后添加方法
### 方法
就像下面的例子一样,必须创建一个对象的新实例 - `Dog` - 并且必须设置`prototype`
```javascript
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;
```
然后必须在Dog原型中添加一个新函数 - `bark()` - 。
### 解
```javascript
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
// Add your code below this line
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function() {
console.log("Woof woof!");
};
// Add your code above this line
let beagle = new Dog();
beagle.eat(); // Should print "nom nom nom"
beagle.bark(); // Should print "Woof!"
```

View File

@@ -0,0 +1,79 @@
---
title: Change the Prototype to a New Object
localeTitle: 将Prototype更改为新对象
---
## 将Prototype更改为新对象
而不是使用`object.prototype.property`添加每个原型属性。通过将原型设置为新对象,我们可以更轻松地完成这项工作。这样,所有原型属性都会立即添加。
## 暗示:
```javascript
Dog.prototype = {
property: value,
functionName: function(){
},
}
```
现在尝试解决挑战!
## Spoiler-Alert解决方案未来
## 解决方案1
```javascript
function Dog(name) {
this.name = name;
}
Dog.prototype = {
// Add your code below this line
numLegs: 2,
eat: function(){
console.log('nom nom nom');
},
describe: function(){
console.log("My name is " + this.name);
}
}
```
## 代码说明:
我们将原型变量分配给一个新对象。 然后我们声明numLegs属性并给它一个值2。
接下来我们创建两个函数“吃”和“描述”。 现在记住对象中的函数是与属性具有相同语法的方法。 您的名称后跟一个值。该值是函数,名称是函数的名称。
## 解决方案2
```javascript
function Dog(name) {
this.name = name;
}
Dog.prototype = {
// Add your code below this line
numLegs: 2,
eat(){
console.log('nom nom nom');
},
describe(){
console.log("My name is " + this.name);
}
};
```
## 代码说明:
这个解决方案和最后一个解决方案之间唯一不同的是我们缩短了“吃”和“描述”功能的语法。 我们通过删除“:”和“功能”一词来做到这一点。
使用ES6我们可以这样做。
你可以在这里阅读: [参考](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions)

View File

@@ -0,0 +1,19 @@
---
title: Create a Basic JavaScript Object
localeTitle: 创建一个基本的JavaScript对象
---
## 创建一个基本的JavaScript对象
### 方法:
这个问题的灵魂或多或少与给出的例子相同。 给`dog`对象两个新属性- `name``numLegs` -并把它们分别设置为一个字符串和一个数字,。
### 解:
```javascript
let dog = {
name: "George",
numLegs: 4
};
```

View File

@@ -0,0 +1,34 @@
---
title: Create a Method on an Object
localeTitle: 在对象上创建方法
---
## 在对象上创建方法
### 方法:
必须在对象本身内初始化对象函数。这在以下代码中进行了演示。
```javascript
let obj = {
property1 = 1,
function1: function() {
//Code to be exectued
}
};
```
### 解:
```javascript
let dog = {
name: "Spot",
numLegs: 4,
sayLegs: function() {
return "This dog has " + dog.numLegs + " legs.";
}
};
dog.sayLegs();
```

View File

@@ -0,0 +1,20 @@
---
title: Define a Constructor Function
localeTitle: 定义构造函数
---
## 定义构造函数
### 方法:
`Dog()`函数应该以与示例中给出的`Bird()`函数完全相同的格式编写。只需将`Bird` with `Dog`替换为通过所有测试用例。
### 解:
```javascript
function Dog() {
this.name = "Geogre",
this.color = "White",
this.numLegs = 4;
}
```

View File

@@ -0,0 +1,21 @@
---
title: Extend Constructors to Receive Arguments
localeTitle: 扩展构造函数以接收参数
---
## 扩展构造函数以接收参数
### 方法:
就像在`Bird()`示例中一样, `Dog()`函数必须使用两个参数 - `name``color` 。然后必须使用`this`关键字在函数内初始化名称和颜色。最终属性 - `numLegs`设置为4因为该函数不接受numLegs参数。
### 解:
```javascript
function Dog(name, color) {
this.name = name;
this.color = color;
this.numLegs = 4;
}
let terrier = new Dog("George","White");
```

View File

@@ -0,0 +1,11 @@
---
title: Object Oriented Programming
localeTitle: 面向对象编程
---
## 面向对象编程
这是一个存根。 [帮助我们的社区扩展它](https://github.com/freecodecamp/guides/tree/master/src/pages/mathematics/quadratic-equations/index.md) 。
[这种快速风格指南有助于确保您的拉取请求被接受](https://github.com/freecodecamp/guides/blob/master/README.md) 。
#### 更多信息:

View File

@@ -0,0 +1,30 @@
---
title: Inherit Behaviors from a Supertype
localeTitle: 从超类型继承行为
---
## 从超类型继承行为
### 方法
要通过此挑战,只需使用以下示例中所示的`Object.create()`方法创建新的`duck``beagle`对象。
\`\`\`的JavaScript
let animal = Object.createAnimal.prototype;
```
### Solution
```
JavaScript的
function Animal{}
Animal.prototype = { 构造函数Animalfunction{ console.log“nom nom nom”; } };
//在此行下方添加代码
let duck = Object.createAnimal.prototype; //改变这一行 让beagle = Object.createAnimal.prototype;; //改变这一行
duck.eat; //应打印“nom nom nom” beagle.eat; //应打印“nom nom nom”
\`\`\`

View File

@@ -0,0 +1,35 @@
---
title: Iterate Over All Properties
localeTitle: 迭代所有属性
---
## 迭代所有属性
### 方法
该方法是使用`for-in-loop`迭代对象中的每个属性。然后在循环内部检查属性是`own-property`还是`prototype` ,并将其放在`ownProps[]`数组或`prototypeProps[]`数组中。请记住`push`属性`push`送到`beagle`对象而不是`Dog`对象以传递所有测试用例。
### 解
```javascript
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
let beagle = new Dog("Snoopy");
let ownProps = [];
let prototypeProps = [];
// Add your code below this line
for (let property in beagle) {
if(Dog.hasOwnProperty(property)) {
ownProps.push(property)
}
else {
prototypeProps.push(property)
}
}
```

View File

@@ -0,0 +1,22 @@
---
title: Make Code More Reusable with the this Keyword
localeTitle: 使用此关键字使代码更可重用
---
## 使用此关键字使代码更可重用
### 方法:
这个挑战只是展示了`this`关键字的强大功能。用`dog.numLegs`替换`dog.numLegs`通过直接引用此对象`this.numLegs`强化我们的代码。 [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)有很多例子来确定`this`关键字的效果。
### 解:
```javascript
let dog = {
name: "Spot",
numLegs: 4,
sayLegs: function() {return "This dog has " + this.numLegs + " legs.";}
};
dog.sayLegs();
```

View File

@@ -0,0 +1,14 @@
---
title: Override Inherited Methods
localeTitle: 覆盖继承的方法
---
## 覆盖继承的方法
# 解
```javascript
Penguin.prototype.fly = function() {
return "Alas, this is a flightless bird.";
};
```

View File

@@ -0,0 +1,25 @@
---
title: Remember to Set the Constructor Property when Changing the Prototype
localeTitle: 请记住在更改原型时设置构造函数属性
---
## 请记住在更改原型时设置构造函数属性
* 在将原型设置为新对象时,请记住定义构造函数属性。
# 解
```javascript
Dog.prototype = {
constructor: Dog, // Solution
numLegs: 2,
eat: function() {
console.log("nom nom nom");
},
describe: function() {
console.log("My name is " + this.name);
}
};
```

View File

@@ -0,0 +1,32 @@
---
title: Reset an Inherited Constructor Property
localeTitle: 重置继承的构造函数属性
---
## 重置继承的构造函数属性
### 方法
`duck``beagle`对象已被编程为继承`supertypes`构造函数属性。要覆盖这两行代码,必须编写将构造函数设置为所需的构造函数`Bird``Dog` 。以下代码演示了如何实现此目的。
```javascript
Bird.prototype.constructor = Bird;
```
### 解
```javascript
function Animal() { }
function Bird() { }
function Dog() { }
Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);
// Add your code below this line
Bird.prototype.constructor = Bird;
Dog.prototype.constructor = Dog;
let duck = new Bird();
let beagle = new Dog();
```

View File

@@ -0,0 +1,35 @@
---
title: Set the Child's Prototype to an Instance of the Parent
localeTitle: 将Child的Prototype设置为Parent的实例
---
## 将Child的Prototype设置为Parent的实例
### 方法
这个挑战与上一个挑战没有什么不同,因为您必须创建一个继承自`supertype`的对象。只有这次子类型`Dog`才会继承`Animal`超类型。 只需创建一个`Dog.prototype`的新实例,如下例所示。
```javascript
Bird.prototype = Object.create(Animal.prototype);
```
### 解
```javascript
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
function Dog() { }
// Add your code below this line
Dog.prototype = Object.create(Animal.prototype);
let beagle = new Dog();
beagle.eat(); // Should print "nom nom nom"
```

View File

@@ -0,0 +1,25 @@
---
title: Understand Own Properties
localeTitle: 了解自己的属性
---
## 了解自己的属性
### 方法:
在给出的示例代码中,您将看到一个新数组`ownProps[]` intialised然后是`for...in`语句循环遍历`duck`的属性,然后使用`push()`语句填充新数组。对于`canary`对象必须遵循相同的方法。
只需将'for ... in'语句中的`duck`对象替换为`canary`对象即可传递所有测试用例。
### 解:
```javascript
let canary = new Bird("Tweety");
let ownProps = [];
// Add your code below this line
for(let property in canary) {
if(canary.hasOwnProperty(property)) {
ownProps.push(property);
}
}
```

View File

@@ -0,0 +1,28 @@
---
title: Understand the Constructor Property
localeTitle: 理解构造函数属性
---
## 理解构造函数属性
### 方法
只需完成类似于给定示例的功能。使用`if-statement`来测试`candidate`是否是`Dog`
### 解
```javascript
function Dog(name) {
this.name = name;
}
// Add your code below this line
function joinDogFraternity(candidate) {
if(candidate.constructor === Dog) {
return true;
}
else {
return false;
}
}
```

View File

@@ -0,0 +1,18 @@
---
title: Understand the Immediately Invoked Function Expression (IIFE)
localeTitle: 理解立即调用的函数表达式IIFE
---
## 理解立即调用的函数表达式IIFE
### 方法
第一个测试用例要求您使该函数匿名。为此,只需删除函数的名称,如示例中所示。然后必须将该函数用大括号括起来,最后用另一组大括号来立即调用该函数。
### 解
```javascript
(function() {
console.log("A cozy nest is ready");
})();
```

View File

@@ -0,0 +1,17 @@
---
title: Understand the Prototype Chain
localeTitle: 理解原型链
---
## 理解原型链
### 解
您的代码应该显示Object.prototype是Dog.prototype的原型
\`\`\`\`javascript function Dogname{ this.name = name; }
让beagle =新狗(“史努比”);
Dog.prototype.isPrototypeOf小猎犬; // =>是的
//修复下面的代码使其评估为true Object.prototype.isPrototypeOfDog.prototype; \`\`\`

View File

@@ -0,0 +1,9 @@
---
title: Understand Where an Objects Prototype Comes From
localeTitle: 了解对象原型的来源
---
## 了解对象原型的来源
这是一个存根。 [帮助我们的社区扩展它](https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/javascript-algorithms-and-data-structures/object-oriented-programming/understand-where-an-objects-prototype-comes-from/index.md) 。
[这种快速风格指南有助于确保您的拉取请求被接受](https://github.com/freecodecamp/guides/blob/master/README.md) 。

View File

@@ -0,0 +1,22 @@
---
title: Use a Constructor to Create Objects
localeTitle: 使用构造函数创建对象
---
## 使用构造函数创建对象
### 方法:
我们在最后的挑战中看到了如何创建构造函数。现在我们可以简单地调用此函数来创建一个具有已在构造函数中定义的属性的新对象。只需初始化一个调用`Dog()`构造函数的新变量`hound`
### 解:
```javascript
function Dog() {
this.name = "Rupert";
this.color = "brown";
this.numLegs = 4;
}
// Add your code below this line
let hound = new Dog();
```

View File

@@ -0,0 +1,33 @@
---
title: Use a Mixin to Add Common Behavior Between Unrelated Objects
localeTitle: 使用Mixin在不相关的对象之间添加常见行为
---
## 使用Mixin在不相关的对象之间添加常见行为
### 方法
就像`flyMixin`函数一样,必须使用新的`glideMixin`函数来接受`bird``boat`对象作为参数。使用与`flyMixin`函数相同的语法创建此新函数,然后在两个对象上调用该函数。
### 解
```javascript
let bird = {
name: "Donald",
numLegs: 2
};
let boat = {
name: "Warrior",
type: "race-boat"
};
// Add your code below this line
let glideMixin = function(obj) {
obj.glide = function() {
console.log("Gliding!");
}
};
glideMixin(bird);
glideMixin(boat);
```

View File

@@ -0,0 +1,45 @@
---
title: Use an IIFE to Create a Module
localeTitle: 使用IIFE创建模块
---
## 使用IIFE创建模块
### 方法
两个`Mixin`都必须包含在一个新的`funModule`所以一个`funModule`的起点是到目前为止注释掉所有的代码。
```javascript
/*let isCuteMixin = function(obj) {
obj.isCute = function() {
return true;
};
};
let singMixin = function(obj) {
obj.sing = function() {
console.log("Singing to an awesome tune");
};
};
*/
```
然后在下面开始编写新的`funModule`代码。在新模块中您需要编写一个return语句来返回两个`Mixin`代码块。只需将原始的`Mixin`代码块复制到新的模块代码中但请记住将两个mixin与a分开`,`
### 解
```javascript
let funModule = (function() {
return {
isCuteMixin: function(obj) {
obj.isCute = function() {
return true;
};
},
singMixin: function(obj) {
obj.sing = function() {
console.log("Singing to an awesome tune");
};
}
}
})();
```

View File

@@ -0,0 +1,23 @@
---
title: Use Closure to Protect Properties Within an Object from Being Modified Externally
localeTitle: 使用闭包保护对象内的属性不被外部修改
---
## 使用闭包保护对象内的属性不被外部修改
### 方法
就像在给出的示例中一样,不是使用`this`关键字声明`weight`变量,而是必须使用`let`关键字将其声明为私有变量。这样它只能在`Bird`函数内访问。然后必须在`Bird`函数内添加`getWeight`方法以访问`weight`变量。
### 解
```javascript
function Bird() {
let weight = 15;
this.getWeight = function() {
return weight;
};
}
```

View File

@@ -0,0 +1,33 @@
---
title: Use Dot Notation to Access the Properties of an Object
localeTitle: 使用点表示法访问对象的属性
---
## 使用点表示法访问对象的属性
### 方法:
以下代码将简单地从`obj`对象中打印`property1`
```javascript
let obj = {
property1 = 1,
property2 = 2
};
console.log(obj.property1);
```
遵循此逻辑,使用`console.log`操作将`property1``property2`都打印到屏幕。
### 解:
```javascript
let dog = {
name: "Spot",
numLegs: 4
};
// Add your code below this line
console.log(dog.name);
console.log(dog.numLegs);
```

View File

@@ -0,0 +1,37 @@
---
title: Use Inheritance So You Don't Repeat Yourself
localeTitle: 使用继承,所以你不要重复自己
---
## 使用继承,所以你不要重复自己
### 解
从Cat.prototype和Bear.prototype中删除“eat”方法并将其添加到Animal.prototype中。
```javascript
function Cat(name) {
this.name = name;
};
Cat.prototype = {
constructor: Cat
};
function Bear(name) {
this.name = name;
};
Bear.prototype = {
constructor: Bear
};
function Animal() { };
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
```

View File

@@ -0,0 +1,31 @@
---
title: Use Prototype Properties to Reduce Duplicate Code
localeTitle: 使用原型属性来减少重复代码
---
## 使用原型属性来减少重复代码
### 方法:
`prototype`属性允许我们从原始代码块外部向对象构造函数添加新属性。 prototype属性还允许您向对象构造函数添加新函数。以下代码演示了如何在对象上使用`.prototype`在构造函数中创建新属性。
#### 例:
```javascript
Obj.prototype.newProperty = "New Property!";
```
使用此逻辑,只需为`numLegs`创建一个新的`prototype`属性。测试用例可以通过更换被传递`Bird`与对象`Dog`给出的示例对象- `Bird.prototype.numLegs = 2;`
### 解:
```javascript
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
// Add your code above this line
let beagle = new Dog("Snoopy");
```

View File

@@ -0,0 +1,32 @@
---
title: Verify an Object's Constructor with instanceof
localeTitle: 使用instanceof验证Object的构造函数
---
## 使用instanceof验证Object的构造函数
### 方法:
就像在上一次挑战中一样,使用给定的构造函数创建一个新对象 - `myHouse`
#### 例:
```javascript
let hound = new Dog();
```
请记住为`House`功能提供一个参数来初始化房间数量。然后只需调用`instanceof`运算符即可在新的House上返回true。
### 解:
```javascript
/* jshint expr: true */
function House(numBedrooms) {
this.numBedrooms = numBedrooms;
}
// Add your code below this line
let myHouse = new House(5);
myHouse instanceof House;
```