前端開發(fā)不得不知的10個(gè)最佳ES6特性
為了保證可讀性,本文采用意譯而非直譯,并且對(duì)源代碼進(jìn)行了大量修改。另外,本文版權(quán)歸原作者所有,翻譯僅用于學(xué)習(xí)。
ES6,正式名稱是ECMAScript2015,但是ES6這個(gè)名稱更加簡(jiǎn)潔。ES6已經(jīng)不再是JavaScript最新的標(biāo)準(zhǔn),但是它已經(jīng)廣泛用于編程實(shí)踐中。如果你還沒用過ES6,現(xiàn)在還不算太晚…
下面是10個(gè)ES6最佳特性,排名不分先后:
- 函數(shù)參數(shù)默認(rèn)值
- 模板字符串
- 多行字符串
- 解構(gòu)賦值
- 對(duì)象屬性簡(jiǎn)寫
- 箭頭函數(shù)
- Promise
- Let與Const
- 類
- 模塊化
- 函數(shù)參數(shù)默認(rèn)值
- 不使用ES6
為函數(shù)的參數(shù)設(shè)置默認(rèn)值:
function foo(height, color)
{
var height = height || 50;
var color = color || 'red';
//...
}
這樣寫一般沒問題,但是,當(dāng)參數(shù)的布爾值為false時(shí),是會(huì)出事情的!比如,我們這樣調(diào)用foo函數(shù):
foo(0, "", "")
因?yàn)?的布爾值為false,這樣height的取值將是50。同理color的取值為‘red'。
使用ES6
function foo(height = 50, color = 'red')
{
// ...
}
模板字符串
不使用ES6
使用+號(hào)將變量拼接為字符串:
var name = 'Your name is ' + first + ' ' + last + '.'
使用ES6
將變量放在大括號(hào)之中:
var name = `Your name is ${first} ${last}.`
ES6的寫法更加簡(jiǎn)潔、直觀。
多行字符串
不使用ES6
使用“\n\t”將多行字符串拼接起來:
var roadPoem = 'Then took the other, as just as fair,\n\t' + 'And having perhaps the better claim\n\t' + 'Because it was grassy and wanted wear,\n\t' + 'Though as for that the passing there\n\t' + 'Had worn them really about the same,\n\t'
使用ES6
將多行字符串放在反引號(hào)“之間就好了:
var roadPoem = `Then took the other, as just as fair, And having perhaps the better claim Because it was grassy and wanted wear, Though as for that the passing there Had worn them really about the same,`
解構(gòu)賦值
不使用ES6
當(dāng)需要獲取某個(gè)對(duì)象的屬性值時(shí),需要單獨(dú)獲?。?/p>
var data = $('body').data(); // data有house和mouse屬性
var house = data.house;
var mouse = data.mouse;
使用ES6
一次性獲取對(duì)象的子屬性:
var { house, mouse} = $('body').data()
對(duì)于數(shù)組也是一樣的:
var [col1, col2] = $('.column');
對(duì)象屬性簡(jiǎn)寫
不使用ES6
對(duì)象中必須包含屬性和值,顯得非常多余:
var bar = 'bar';
var foo = function ()
{
// ...
}
var baz = {
bar: bar,
foo: foo
};
使用ES6
對(duì)象中直接寫變量,非常簡(jiǎn)單:
var bar = 'bar';
var foo = function ()
{
// ...
}
var baz = { bar, foo };
箭頭函數(shù)
不使用ES6
普通函數(shù)體內(nèi)的this,指向調(diào)用時(shí)所在的對(duì)象。
function foo()
{
console.log(this.id);
}
var id = 1;
foo(); // 輸出1
foo.call({ id: 2 }); // 輸出2
使用ES6
箭頭函數(shù)體內(nèi)的this,就是定義時(shí)所在的對(duì)象,而不是調(diào)用時(shí)所在的對(duì)象。
var foo = () => {
console.log(this.id);
}
var id = 1;
foo(); // 輸出1
foo.call({ id: 2 }); // 輸出1
Promise
不使用ES6
嵌套兩個(gè)setTimeout回調(diào)函數(shù):
setTimeout(function()
{
console.log('Hello'); // 1秒后輸出"Hello"
setTimeout(function()
{
console.log('Fundebug'); // 2秒后輸出"Fundebug"
}, 1000);
}, 1000);
使用ES6
使用兩個(gè)then是異步編程串行化,避免了回調(diào)地獄:
var wait1000 = new Promise(function(resolve, reject)
{
setTimeout(resolve, 1000);
});
wait1000
.then(function()
{
console.log("Hello"); // 1秒后輸出"Hello"
return wait1000;
})
.then(function()
{
console.log("Fundebug"); // 2秒后輸出"Fundebug"
});
Let與Const
使用Var
var定義的變量未函數(shù)級(jí)作用域:
{
var a = 10;
}
console.log(a); // 輸出10
使用let與const
let定義的變量為塊級(jí)作用域,因此會(huì)報(bào)錯(cuò):(如果你希望實(shí)時(shí)監(jiān)控JavaScript應(yīng)用的錯(cuò)誤,歡迎免費(fèi)使用Fundebug)
{
let a = 10;
}
console.log(a); // 報(bào)錯(cuò)“ReferenceError: a is not defined”
const與let一樣,也是塊級(jí)作用域。
類
不使用ES6
使用構(gòu)造函數(shù)創(chuàng)建對(duì)象:
function Point(x, y)
{
this.x = x;
this.y = y;
this.add = function()
{
return this.x + this.y;
};
}
var p = new Point(1, 2);
console.log(p.add()); // 輸出3
使用ES6
使用Class定義類,更加規(guī)范,且你能夠繼承:
class Point
{
constructor(x, y)
{
this.x = x;
this.y = y;
}
add()
{
return this.x + this.y;
}
}
var p = new Point(1, 2);
console.log(p.add()); // 輸出3
模塊化
JavaScript一直沒有官方的模塊化解決方案,開發(fā)者在實(shí)踐中主要采用CommonJS和AMD規(guī)范。而ES6制定了模塊(Module)功能。
不使用ES6
Node.js采用CommenJS規(guī)范實(shí)現(xiàn)了模塊化,而前端也可以采用,只是在部署時(shí)需要使用Browserify等工具打包。這里不妨介紹一下CommenJS規(guī)范。
module.js中使用module.exports導(dǎo)出port變量和getAccounts函數(shù):
module.exports = {
port: 3000,
getAccounts: function() {
...
}
}
main.js中使用require導(dǎo)入module.js:
var service = require('module.js')
console.log(service.port) // 輸出3000
使用ES6
ES6中使用export與import關(guān)鍵詞實(shí)現(xiàn)模塊化。
module.js中使用export導(dǎo)出port變量和getAccounts函數(shù):
export var port = 3000
export function getAccounts(url) {
...
}
main.js中使用import導(dǎo)入module.js,可以指定需要導(dǎo)入的變量:
import {port, getAccounts} from 'module'
console.log(port) // 輸出3000
也可以將全部變量導(dǎo)入:
import * as service from 'module' console.log(service.port) // 3000
總結(jié)
以上所述是小編給大家介紹的前端開發(fā)不得不知的10個(gè)最佳ES6特性,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- ES6使用新特性Proxy實(shí)現(xiàn)的數(shù)據(jù)綁定功能實(shí)例
- JS基于ES6新特性async await進(jìn)行異步處理操作示例
- ES6中l(wèi)et 和 const 的新特性
- es6新特性之 class 基本用法解析
- ES6新特性:使用export和import實(shí)現(xiàn)模塊化詳解
- 讓微信小程序支持ES6中Promise特性的方法詳解
- ES6新特性之類(Class)和繼承(Extends)相關(guān)概念與用法分析
- ES6新特性八:async函數(shù)用法實(shí)例詳解
- ES6新特性七:數(shù)組的擴(kuò)充詳解
- ES6 十大特性簡(jiǎn)介
相關(guān)文章
JS路由跳轉(zhuǎn)的簡(jiǎn)單實(shí)現(xiàn)代碼
本文給大家分享一個(gè)簡(jiǎn)單的js路由跳轉(zhuǎn)功能,非常不錯(cuò),需要的朋友參考下吧2017-09-09
offsetHeight在OnLoad中獲取為0的現(xiàn)象
需要獲取div的高度時(shí),往往需要用到offsetHeight,有時(shí)會(huì)碰到offsetHeight獲取到為0的現(xiàn)象,感興趣的朋友可以參考下面的代碼片段2013-07-07
JavaScript正則表達(dá)式的貪婪匹配和非貪婪匹配
所謂貪婪匹配就是匹配重復(fù)字符是盡可能多的匹配,非貪婪匹配就是盡可能少的匹配,下面通過一個(gè)例子給大家分享JavaScript正則表達(dá)式的貪婪匹配和非貪婪匹配,感興趣的朋友參考下吧2017-09-09
使用UrlConnection實(shí)現(xiàn)后臺(tái)模擬http請(qǐng)求的簡(jiǎn)單實(shí)例
這篇文章主要介紹了使用UrlConnection實(shí)現(xiàn)后臺(tái)模擬http請(qǐng)求的簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01
js實(shí)現(xiàn)翻頁(yè)后保持checkbox選中狀態(tài)的實(shí)現(xiàn)方法
在項(xiàng)目中有需求如下:上下分頁(yè)后,選中的checkbox狀態(tài)保持不變2012-11-11

