const student = { name: 'cc' }
student.name = 'yy';// 不報錯
student = { name: 'yy' };// 報錯
複製代碼
var tmp = 123;
if (true) {
tmp = 'abc'; // ReferenceError
let tmp;
}
複製代碼
tmp
,可是塊級做用域內let
又聲明瞭一個局部變量tmp
,致使後者綁定這個塊級做用域,因此在let
聲明變量前,對tmp
賦值會報錯。有幾個點須要注意:前端
# let 關鍵詞聲明的變量不具有變量提高(hoisting)特性
# let 和 const 聲明只在最靠近的一個塊中(花括號內)有效
# 當使用常量 const 聲明時,請使用大寫變量,如:CAPITAL_CASING
# const 在聲明時必須被賦值
複製代碼
在平常開發中,個人建議是全面擁抱let/const,通常的變量聲明使用let關鍵字,而當聲明一些配置項(相似接口地址,npm依賴包,分頁器默認頁數等一些一旦聲明後就不會改變的變量)的時候可使用const,來顯式的告訴項目其餘開發者,這個變量是不能改變的(const聲明的常量建議使用全大寫字母標識,單詞間用下劃線),同時也建議瞭解var關鍵字的缺陷(變量提高,污染全局變量等),這樣才能更好的使用新語法vue
# 不須要 function 關鍵字來建立函數
# 省略 return 關鍵字
# 繼承當前上下文的 this 關鍵字
複製代碼
// ES6以前,當未傳入參數時,text = 'default';
function printText(text) {
text = text || 'default';
console.log(text);
}
// ES6;
function printText(text = 'default') {
console.log(text);
}
printText('hello'); // hello
printText();// default
複製代碼
Promise做爲ES6中推出的新的概念,改變了JS的異步編程,現代前端大部分的異步請求都是使用Promise實現,fetch這個web api也是基於Promise的,這裏不得簡述一下以前統治JS異步編程的回調函數,回調函數有什麼缺點,Promise又是怎麼改善這些缺點react
針對回調函數這麼多缺點,ES6中引入了一個新的概念Promise,Promise是一個構造函數,經過new關鍵字建立一個Promise的實例,來看看Promise是怎麼解決回調函數的這些問題es6
// bad
const foo = 'this is a' + example;
// good
const foo = `this is a ${example}`;
複製代碼
在ES6 Module出現以前,模塊化一直是前端開發者討論的重點,面對日益增加的需求和代碼,須要一種方案來將臃腫的代碼拆分紅一個個小模塊,從而推出了AMD,CMD和CommonJs這3種模塊化方案,前者用在瀏覽器端,後面2種用在服務端,直到ES6 Module出現web
心得:ES6不只支持變量的導出,也支持常量的導出。 export const sqrt = Math.sqrt;//導出常量ajax
心得:一條import 語句能夠同時導入默認函數和其它變量。import default Method, { otherMethod } from 'xxx.js';vuex
解構賦值能夠直接使用對象的某個屬性,而不須要經過屬性訪問的形式使用,對象解構原理我的認爲是經過尋找相同的屬性名,而後原對象的這個屬性名的值賦值給新對象對應的屬性, 鍵找鍵,找到了就賦值了npm
var arr = [1, 2, 3, 4];
let [a, b, c, d] = arr;
console.log(a); // 1
console.log(b); // 2
複製代碼
var luke = { occupation: 'jedi', father: 'anakin' };
let {occupation, father} = luke;
console.log(occupation); // jedi
console.log(father); // anakin
複製代碼
一樣建議使用,由於解構賦值語意化更強,對於做爲對象的函數參數來講,能夠減小形參的聲明,直接使用對象的屬性(若是嵌套層數過多我我的認爲不適合用對象解構,不太優雅)編程
剩餘/擴展運算符一樣也是ES6一個很是重要的語法,使用3個點(...),後面跟着一個含有iterator接口的數據結構c#
剩餘運算符最重要的一個特色就是替代了之前的arguments
rest只是形參, 能夠隨意取名
剩餘運算符和擴展運算符的區別就是,剩餘運算符會收集這些集合,放到右邊的數組中,擴展運算符是將右邊的數組拆分紅元素的集合,它們是相反的
對熟悉Java,object-c,c#等純面嚮對象語言的開發者來講,都會對class有一種特殊的情懷。ES6 引入了class(類),讓JavaScript的面向對象編程變得更加簡單和易於理解。
class Animal {
// 構造函數,實例化的時候將會被調用,若是不指定,那麼會有一個不帶參數的默認構造函數.
constructor(name,color) {
this.name = name;
this.color = color;
}
// toString 是原型對象上的屬性
toString() {
console.log('name:' + this.name + ',color:' + this.color);
}
}
var animal = new Animal('dog','white');//實例化Animal
animal.toString();
console.log(animal.hasOwnProperty('name')); //true
console.log(animal.hasOwnProperty('toString')); // false
console.log(animal.__proto__.hasOwnProperty('toString')); // true
class Cat extends Animal {
constructor(action) {
// 子類必需要在constructor中指定super 函數,不然在新建實例的時候會報錯.
// 若是沒有置頂consructor,默認帶super函數的constructor將會被添加、
super('cat','white');
this.action = action;
}
toString() {
console.log(super.toString());
}
}
var cat = new Cat('catch')
cat.toString();
// 實例cat 是 Cat 和 Animal 的實例,和Es5徹底一致。
console.log(cat instanceof Cat); // true
console.log(cat instanceof Animal); // true
複製代碼
includes() 函數用來判斷一個數組是否包含一個指定的值,若是包含則返回 true,不然返回false。
includes 函數與 indexOf 函數很類似,下面兩個表達式是等價的:
arr.includes(x)
arr.indexOf(x) >= 0
複製代碼
接下來咱們來判斷數字中是否包含某個元素:
在ES7以前的作法
使用indexOf()驗證數組中是否存在某個元素,這時須要根據返回值是否爲-1來判斷:
let arr = ['react', 'angular', 'vue'];
if (arr.indexOf('react') !== -1)
{
console.log('react存在');
}
複製代碼
使用ES7的includes()
使用includes()驗證數組中是否存在某個元素,這樣更加直觀簡單:
let arr = ['react', 'angular', 'vue'];
if (arr.includes('react'))
{
console.log('react存在');
}
複製代碼
在ES7中引入了指數運算符**
,**
具備與Math.pow(..)
等效的計算結果。
不使用指數操做符
使用自定義的遞歸函數calculateExponent或者Math.pow()進行指數運算:
function calculateExponent(base, exponent)
{
if (exponent === 1)
{
return base;
}
else
{
return base * calculateExponent(base, exponent - 1);
}
}
console.log(calculateExponent(2, 10)); // 輸出1024
console.log(Math.pow(2, 10)); // 輸出1024
複製代碼
複製代碼
使用指數操做符
使用指數運算符**,就像+、-等操做符同樣:
console.log(2**10);// 輸出1024
複製代碼
1