日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
關于ES6的10個突出特性

ES6,正式名稱是ECMAScript2015,但是ES6這個名稱更加簡潔。ES6已經(jīng)不再是JavaScript***的標準,但是它已經(jīng)廣泛用于編程實踐中。如果你還沒用過ES6,現(xiàn)在還不算太晚...

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設,承德縣企業(yè)網(wǎng)站建設,承德縣品牌網(wǎng)站建設,網(wǎng)站定制,承德縣網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,承德縣網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

下面是10個ES6突出特性,排名不分先后:

  • 函數(shù)參數(shù)默認值
  • 模板字符串
  • 多行字符串
  • 解構賦值
  • 對象屬性簡寫
  • 箭頭函數(shù)
  • Promise
  • Let與Const
  • 模塊化

1. 函數(shù)參數(shù)默認值

不使用ES6

為函數(shù)的參數(shù)設置默認值:

 
 
 
 
  1. function foo(height, color) 
  2.     var height = height || 50; 
  3.     var color = color || 'red'; 
  4.     //... 
  5. }  

這樣寫一般沒問題,但是,當參數(shù)的布爾值為false時,是會出事情的!比如,我們這樣調用foo函數(shù):

 
 
 
 
  1. foo(0, "", "") 

因為0的布爾值為false,這樣height的取值將是50。同理color的取值為'red'。

使用ES6

 
 
 
 
  1. function foo(height = 50, color = 'red') 
  2.     // ... 
  3. }  

2. 模板字符串

不使用ES6

使用+號將變量拼接為字符串:

 
 
 
 
  1. var name = 'Your name is ' + first + ' ' + last + '.' 

使用ES6

將變量放在大括號之中:

 
 
 
 
  1. var name = `Your name is ${first} ${last}.` 

ES6的寫法更加簡潔、直觀。

3. 多行字符串

不使用ES6

使用"nt"將多行字符串拼接起來:

 
 
 
 
  1. var roadPoem = 'Then took the other, as just as fair,\n\t' 
  2.     + 'And having perhaps the better claim\n\t' 
  3.     + 'Because it was grassy and wanted wear,\n\t' 
  4.     + 'Though as for that the passing there\n\t' 
  5.     + 'Had worn them really about the same,\n\t'  

使用ES6

將多行字符串放在反引號``之間就好了:

 
 
 
 
  1. var roadPoem = `Then took the other, as just as fair,  
  2. And having perhaps the better claim  
  3. Because it was grassy and wanted wear,  
  4. Though as for that the passing there  
  5. Had worn them really about the same,` 

4. 解構賦值

不使用ES6

當需要獲取某個對象的屬性值時,需要單獨獲?。?/p>

 
 
 
 
  1. var data = $('body').data(); // data有house和mouse屬性 
  2. var house = data.house; 
  3. var mouse = data.mouse;  

使用ES6

一次性獲取對象的子屬性:

 
 
 
 
  1. var { house, mouse} = $('body').data() 

對于數(shù)組也是一樣的:

 
 
 
 
  1. var [col1, col2] = $('.column'); 

5. 對象屬性簡寫

不使用ES6

對象中必須包含屬性和值,顯得非常多余:

 
 
 
 
  1. var bar = 'bar'; 
  2. var foo = function () 
  3.     // ... 
  4.  
  5. var baz = { 
  6.   bar: bar, 
  7.   foo: foo 
  8. };  

使用ES6

對象中直接寫變量,非常簡單:

 
 
 
 
  1. var bar = 'bar'; 
  2. var foo = function () 
  3.     // ... 
  4.  
  5. var baz = { bar, foo };  

6. 箭頭函數(shù)

不使用ES6

普通函數(shù)體內的this,指向調用時所在的對象。

 
 
 
 
  1. function foo()  
  2.     console.log(this.id); 
  3.  
  4. var id = 1; 
  5.  
  6. foo(); // 輸出1 
  7.  
  8. foo.call({ id: 2 }); // 輸出2  

使用ES6

箭頭函數(shù)體內的this,就是定義時所在的對象,而不是調用時所在的對象。

 
 
 
 
  1. var foo = () => { 
  2.   console.log(this.id); 
  3.  
  4. var id = 1; 
  5.  
  6. foo(); // 輸出1 
  7.  
  8. foo.call({ id: 2 }); // 輸出1  

7. Promise

不使用ES6

嵌套兩個setTimeout回調函數(shù):

 
 
 
 
  1. setTimeout(function() 
  2.     console.log('Hello'); // 1秒后輸出"Hello" 
  3.     setTimeout(function() 
  4.     { 
  5.         console.log('Fundebug'); // 2秒后輸出"Fundebug" 
  6.     }, 1000); 
  7. }, 1000);  

使用ES6

使用兩個then是異步編程串行化,避免了回調地獄:

 
 
 
 
  1. var wait1000 = new Promise(function(resolve, reject) 
  2.     setTimeout(resolve, 1000); 
  3. }); 
  4.  
  5. wait1000 
  6.     .then(function() 
  7.     { 
  8.         console.log("Hello"); // 1秒后輸出"Hello" 
  9.         return wait1000; 
  10.     }) 
  11.     .then(function() 
  12.     { 
  13.         console.log("Fundebug"); // 2秒后輸出"Fundebug" 
  14.     });  

8. Let與Const

使用Var

var定義的變量未函數(shù)級作用域:

 
 
 
 
  1.   var a = 10; 
  2.  
  3. console.log(a); // 輸出10  

使用let與const

let定義的變量為塊級作用域,因此會報錯:(如果你希望實時監(jiān)控JavaScript應用的錯誤,歡迎免費使用Fundebug)

 
 
 
 
  1.   let a = 10; 
  2.  
  3. console.log(a); // 報錯“ReferenceError: a is not defined”  

const與let一樣,也是塊級作用域。

9. 類

不使用ES6

使用構造函數(shù)創(chuàng)建對象:

 
 
 
 
  1. function Point(x, y) 
  2.     this.x = x; 
  3.     this.y = y; 
  4.     this.add = function() 
  5.     { 
  6.         return this.x + this.y; 
  7.     }; 
  8.  
  9. var p = new Point(1, 2); 
  10.  
  11. console.log(p.add()); // 輸出3  

使用ES6

使用Class定義類,更加規(guī)范,且你能夠繼承:

 
 
 
 
  1. class Point 
  2.     constructor(x, y) 
  3.     { 
  4.         this.x = x; 
  5.         this.y = y; 
  6.     } 
  7.  
  8.     add() 
  9.     { 
  10.         return this.x + this.y; 
  11.     } 
  12.  
  13. var p = new Point(1, 2); 
  14.  
  15. console.log(p.add()); // 輸出3  

10. 模塊化

JavaScript一直沒有官方的模塊化解決方案,開發(fā)者在實踐中主要采用CommonJS和AMD規(guī)范。而ES6制定了模塊(Module)功能。

不使用ES6

Node.js采用CommenJS規(guī)范實現(xiàn)了模塊化,而前端也可以采用,只是在部署時需要使用Browserify等工具打包。這里不妨介紹一下CommenJS規(guī)范。

module.js中使用module.exports導出port變量和getAccounts函數(shù):

 
 
 
 
  1. module.exports = { 
  2.   port: 3000, 
  3.   getAccounts: function() { 
  4.     ... 
  5.   } 
  6. }  

main.js中使用require導入module.js:

 
 
 
 
  1. var service = require('module.js') 
  2. console.log(service.port) // 輸出3000  

使用ES6

ES6中使用export與import關鍵詞實現(xiàn)模塊化。

module.js中使用export導出port變量和getAccounts函數(shù):

 
 
 
 
  1. export var port = 3000 
  2. export function getAccounts(url) { 
  3.   ... 
  4. }  

main.js中使用import導入module.js,可以指定需要導入的變量:

 
 
 
 
  1. import {port, getAccounts} from 'module' 
  2. console.log(port) // 輸出3000  

也可以將全部變量導入:

 
 
 
 
  1. import * as service from 'module' 
  2. console.log(service.port) // 3000  

當前題目:關于ES6的10個突出特性
URL分享:http://www.5511xx.com/article/dhsjjsc.html