Skip to content

Commit

Permalink
Merge pull request lin-123#6 from GabrielchenCN/cn
Browse files Browse the repository at this point in the history
feat: update guide
  • Loading branch information
lin-123 committed Dec 28, 2019
2 parents abdcf8f + b6c8cbd commit 3903cea
Showing 1 changed file with 54 additions and 15 deletions.
69 changes: 54 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1200,13 +1200,13 @@ Other Style Guides
}
inherits(PeekableQueue, Queue);
PeekableQueue.prototype.peek = function () {
return this._queue[0];
return this.queue[0];
}

// good
class PeekableQueue extends Queue {
peek() {
return this._queue[0];
return this.queue[0];
}
}
```
Expand Down Expand Up @@ -1325,6 +1325,40 @@ Other Style Guides
}
```

<a name="classes--methods-use-this"></a>
- [9.7](#classes--methods-use-this) 除非外部库或框架需要使用特定的非静态方法,否则类方法应该使用`this`或被做成静态方法。
作为一个实例方法应该表明它根据接收者的属性有不同的行为。eslint: [`class-methods-use-this`](https://eslint.org/docs/rules/class-methods-use-this)

```javascript
// bad
class Foo {
bar() {
console.log('bar');
}
}

// good - this 被使用了
class Foo {
bar() {
console.log(this.bar);
}
}

// good - constructor 不一定要使用this
class Foo {
constructor() {
// ...
}
}

// good - 静态方法不需要使用 this
class Foo {
static bar() {
console.log('bar');
}
}
```

**[⬆ back to top](#目录)**

## Modules
Expand Down Expand Up @@ -1803,14 +1837,14 @@ Other Style Guides
```javascript
// bad

let array = [1, 2, 3];
const array = [1, 2, 3];
let num = 1;
num++;
--num;

let sum = 0;
let truthyCount = 0;
for(let i = 0; i < array.length; i++){
for (let i = 0; i < array.length; i++) {
let value = array[i];
sum += value;
if (value) {
Expand All @@ -1820,7 +1854,7 @@ Other Style Guides

// good

let array = [1, 2, 3];
const array = [1, 2, 3];
let num = 1;
num += 1;
num -= 1;
Expand Down Expand Up @@ -3474,11 +3508,16 @@ Other Style Guides
// ...
];

// also good
const httpRequests = [
// ...
];

// best
import TextMessageContainer from './containers/TextMessageContainer';

// best
const Requests = [
const requests = [
// ...
];
```
Expand Down Expand Up @@ -3506,15 +3545,15 @@ Other Style Guides

// ---

// allowed but does not supply semantic value
// 允许但不够语义化
export const apiKey = 'SOMEKEY';

// better in most cases
// 在大多数情况下更好
export const API_KEY = 'SOMEKEY';

// ---

// bad - unnecessarily uppercases key while adding no semantic value
// bad - 不必要的大写键,没有增加任何语言
export const MAPPING = {
KEY: 'value'
};
Expand Down Expand Up @@ -3612,23 +3651,23 @@ Other Style Guides
// bad
$(this).trigger('listingUpdated', listing.id);

...
// ...

$(this).on('listingUpdated', (e, listingId) => {
// do something with listingId
$(this).on('listingUpdated', (e, listingID) => {
// do something with listingID
});
```

prefer:

```javascript
// good
$(this).trigger('listingUpdated', { listingId: listing.id });
$(this).trigger('listingUpdated', { listingID: listing.id });

...
// ...

$(this).on('listingUpdated', (e, data) => {
// do something with data.listingId
// do something with data.listingID
});
```

Expand Down

0 comments on commit 3903cea

Please sign in to comment.