亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

前端開(kāi)發(fā)中的一些js規(guī)范

 

1.不要用遍歷器。用JAVAScript高級(jí)函數(shù)代替`for-in`、 `for-of`。

const numbers = [1, 2, 3, 4, 5];

// bad

let sum = 0;

for (let num of numbers) {

sum += num;

}

sum === 15;

// good

let sum = 0;

numbers.forEach(num => sum += num);

sum === 15;

// best (use the functional force)

const sum = numbers.reduce((total, num) => total + num, 0);

sum === 15;

// bad

const increasedByOne = [];

for (let i = 0; i < numbers.length; i++) {

increasedByOne.push(numbers[i] + 1);

}

// good

const increasedByOne = [];

numbers.forEach(num => increasedByOne.push(num + 1));

// best (keeping it functional)

const increasedByOne = numbers.map(num => num + 1);

2不要直接調(diào)用`Object.prototype`上的方法,如`hasOwnProperty`, `propertyIsEnumerable`, `isPrototypeOf`。

// bad

console.log(object.hasOwnProperty(key));

// good

console.log(Object.prototype.hasOwnProperty.call(object, key));

3.對(duì)象淺拷貝時(shí),更推薦使用擴(kuò)展運(yùn)算符[就是`...`運(yùn)算符],而不是[`Object.assign`]

// bad

const original = { a: 1, b: 2 };

const copy = Object.assign({}, original, { c: 3 }); // copy => { a: 1, b: 2, c: 3 }

// good es6擴(kuò)展運(yùn)算符 ...

const original = { a: 1, b: 2 };

// 淺拷貝

const copy = { ...original, c: 3 }; // copy => { a: 1, b: 2, c: 3 }

// rest 賦值運(yùn)算符

const { a, ...noA } = copy; // noA => { b: 2, c: 3 }

4.用擴(kuò)展運(yùn)算符做數(shù)組淺拷貝,類似上面的對(duì)象淺拷貝

// bad

const len = items.length;

const itemsCopy = [];

let i;

for (i = 0; i < len; i += 1) {

itemsCopy[i] = items[i];

}

// good

const itemsCopy = [...items];

5. 用 `...` 運(yùn)算符而不是[`Array.from`]

const foo = document.querySelectorAll('.foo');

// good

const nodes = Array.from(foo);

// best

const nodes = [...foo];

6.用對(duì)象的解構(gòu)賦值來(lái)獲取和使用對(duì)象某個(gè)或多個(gè)屬性值。

// bad

function getFullName(user) {

const firstName = user.firstName;

const lastName = user.lastName;

return `${firstName} ${lastName}`;

}

// good

function getFullName(user) {

const { firstName, lastName } = user;

return `${firstName} ${lastName}`;

}

// best

function getFullName({ firstName, lastName }) {

return `${firstName} ${lastName}`;

}

7. const arr = [1, 2, 3, 4];

// bad

const first = arr[0];

const second = arr[1];

// good

const [first, second] = arr;

8.多個(gè)返回值用對(duì)象的解構(gòu),而不是數(shù)據(jù)解構(gòu)。

// bad

function processInput(input) {

// 然后就是見(jiàn)證奇跡的時(shí)刻

return [left, right, top, bottom];

}

// 調(diào)用者需要想一想返回值的順序

const [left, __, top] = processInput(input);

// good

function processInput(input) {

// oops, 奇跡又發(fā)生了

return { left, right, top, bottom };

}

// 調(diào)用者只需要選擇他想用的值就好了

const { left, top } = processInput(input);

9. 用命名函數(shù)表達(dá)式而不是函數(shù)聲明。

// bad

function foo() {

// ...

}

// bad

const foo = function () {

// ...

};

// good

// lexical name distinguished from the variable-referenced invocation(s)

// 函數(shù)表達(dá)式名和聲明的函數(shù)名是不一樣的

const short = function longUniqueMoreDescriptiveLexicalFoo() {

// ...

};

10.不要使用`arguments`,用rest語(yǔ)法`...`代替。

// bad

function concatenateAll() {

const args = Array.prototype.slice.call(arguments);

return args.join('');

}

// good

function concatenateAll(...args) {

return args.join('');

}

11.用`spread`操作符`...`去調(diào)用多變的函數(shù)更好

// bad

const x = [1, 2, 3, 4, 5];

console.log.Apply(console, x);

// good

const x = [1, 2, 3, 4, 5];

console.log(...x);

// bad

new (Function.prototype.bind.apply(Date, [null, 2016, 8, 5]));

// good

new Date(...[2016, 8, 5]);

12.當(dāng)你一定要用函數(shù)表達(dá)式(在回調(diào)函數(shù)里)的時(shí)候就用箭頭表達(dá)式吧。

// bad

[1, 2, 3].map(function (x) {

const y = x + 1;

return x * y;

});

// good

[1, 2, 3].map((x) => {

const y = x + 1;

return x * y;

});

13.常用`class`,避免直接操作`prototype`

// bad

function Queue(contents = []) {

this.queue = [...contents];

}

Queue.prototype.pop = function () {

const value = this.queue[0];

this.queue.splice(0, 1);

return value;

};

// good

class Queue {

constructor(contents = []) {

this.queue = [...contents];

}

pop() {

const value = this.queue[0];

this.queue.splice(0, 1);

return value;

}

}

14. 用`extends`實(shí)現(xiàn)繼承

// bad

const inherits = require('inherits');

function PeekableQueue(contents) {

Queue.apply(this, contents);

}

inherits(PeekableQueue, Queue);

PeekableQueue.prototype.peek = function () {

return this._queue[0];

}

// good

class PeekableQueue extends Queue {

peek() {

return this._queue[0];

}

}

15.可以返回`this`來(lái)實(shí)現(xiàn)方法鏈

// bad

Jedi.prototype.jump = function () {

this.jumping = true;

return true;

};

Jedi.prototype.setHeight = function (height) {

this.height = height;

};

const luke = new Jedi();

luke.jump(); // => true

luke.setHeight(20); // => undefined

// good

class Jedi {

jump() {

this.jumping = true;

return this;

}

setHeight(height) {

this.height = height;

return this;

}

}

const luke = new Jedi();

luke.jump()

.setHeight(20);

16.如果沒(méi)有具體說(shuō)明,類有默認(rèn)的構(gòu)造方法。一個(gè)空的構(gòu)造函數(shù)或只是代表父類的構(gòu)造函數(shù)是不需要寫的。

// bad

class Jedi {

constructor() {}

getName() {

return this.name;

}

}

// bad

class Rey extends Jedi {

// 這種構(gòu)造函數(shù)是不需要寫的

constructor(...args) {

super(...args);

}

}

// good

class Rey extends Jedi {

constructor(...args) {

super(...args);

this.name = 'Rey';

}

}

17. 一個(gè)路徑只 import 一次。

// bad

import foo from 'foo';

// … some other imports … //

import { named1, named2 } from 'foo';

// good

import foo, { named1, named2 } from 'foo';

// good

import foo, {

named1,

named2,

} from 'foo';

18. 做冪運(yùn)算時(shí)用冪操作符 `**` 。

// bad

const binary = Math.pow(2, 10);

// good

const binary = 2 ** 10;

19.三元表達(dá)式不應(yīng)該嵌套,通常是單行表達(dá)式。

// bad

const foo = maybe1 > maybe2

? "bar"

: value1 > value2 ? "baz" : null;

// better

const maybeNull = value1 > value2 ? 'baz' : null;

const foo = maybe1 > maybe2

? 'bar'

: maybeNull;

// best

const maybeNull = value1 > value2 ? 'baz' : null;

const foo = maybe1 > maybe2 ? 'bar' : maybeNull;

20.避免不需要的三元表達(dá)式

// bad

const foo = a ? a : b;

const bar = c ? true : false;

const baz = c ? false : true;

// good

const foo = a || b;

const bar = !!c;

const baz = !c;

21. 如果 `if` 語(yǔ)句中總是需要用 `return` 返回, 那后續(xù)的 `else` 就不需要寫了。 `if` 塊中包含 `return`, 它后面的 `else if` 塊中也包含了 `return`, 這個(gè)時(shí)候就可以把 `return` 分到多個(gè) `if` 語(yǔ)句塊中。

// bad

function foo() {

if (x) {

return x;

} else {

return y;

}

}

// bad

function cats() {

if (x) {

return x;

} else if (y) {

return y;

}

}

// bad

function dogs() {

if (x) {

return x;

} else {

if (y) {

return y;

}

}

}

// good

function foo() {

if (x) {

return x;

}

return y;

}

// good

function cats() {

if (x) {

return x;

}

if (y) {

return y;

}

}

// good

function dogs(x) {

if (x) {

if (z) {

return y;

}

} else {

return z;

}

}

分享到:
標(biāo)簽:規(guī)范 js
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定