-
https://github.cm/cucygh/fe-material查看全部
-
//ES6對象 let?Person?=?{ ????name:'haha', ????sex:'female', ????age:18 } let?person?=?new?Proxy(Person,{ ????get(target,key){ ????????return?target[key]; ????}, ????set(target,key,value){ ????????if(key?!==?'sex'){ ????????????target[key]?=?value; ????????} ????????return?target[key]; ????} })
查看全部 -
ES5數(shù)組合并用數(shù)組的concat()方法,ES6利用擴展運算符
查看全部 -
...a是ES6中的可變參數(shù),在不確定的時候傳值方便
查看全部 -
《ECMAScript 6 入門》
http://es6.ruanyifeng.com/
查看全部 -
https://github.com/cucygh/fe-material
查看全部 -
常量
作用域
箭頭函數(shù)
默認參數(shù)
對象代理
查看全部 -
好基礎(chǔ),隨便看10分鐘書就結(jié)束了
查看全部 -
es6已經(jīng)成為了各大框架的基礎(chǔ),不錯不錯,有點意思
查看全部 -
一、對象代理
什么是對象代理?比如我們封裝了一組數(shù)據(jù),通過訪問代理,對數(shù)據(jù)訪問做限制,而用戶訪問的是代理層,而不是源數(shù)據(jù)。這樣就數(shù)據(jù)進行保護。
二、es3,es5,es6中的對象代理
{ ??//?es3,閉包方式保護 ??var?Person?=?function()?{ ????var?data?=?{ ??????name:?'es3', ??????sex:?'male', ??????age:?15 ????} ????this.get?=?function(key)?{ ??????return?data[key]; ????}; ????this.set?=?function(key)?{ ??????if?(key?!=?'sex')?{ ????????data[key]?=?value; ??????} ????}; ??} ??//?聲明一個實例 ??var?person?=?new?Person(); ??//?讀取 ??console.table({ ????name:?person.get('name'), ????sex:?person.get('sex'), ????age:?person.get('age') ????} ??); ??//?修改 ??person.set('name',?'es3-cname'); ??console.table({ ??????name:?person.get('name'), ??????sex:?person.get('sex'), ??????age:?person.get('age') ????} ??); ??person.set('sex',?'female'); ??console.table({ ??????name:?person.get('name'), ??????sex:?person.get('sex'), ??????age:?person.get('age') ????} ??); } { ??//?es5 ??var?Person?=?{ ????name:?'es5', ????age:?15, ????sex:?'male' ??} ??Object.defineProperty(Person,?'sex',?{ ????writable:?false, ????value:?'male' ??}); ??console.table({ ????name:?Person.name, ????sex:?Person.sex, ????age:?Person.age ??}); ??Person.sex?=?'female'; ??//?如果改變sex的值,會報錯:cannot?assign?to?read?only?property?'sex'?of?object?'#<object>',為了不影響執(zhí)行,可用try...catch } { ??//?es6 ??let?Person?=?{ ????name:?'es6', ????sex:?'male', ????age:?15 ??}; ??let?Person?=?new?Proxy(Person,?{?//?ES6原生語法代理?:?let??object?=?new?Proxy()?;??object?為?用戶訪問操作的代理對象,而不是原始對象 ????get(target,?key)?{ ??????return?target[key]; ????}, ????set(target,?key)?{?//?既不影響get,又與業(yè)務(wù)代碼隔離 ??????if?(key?!==?'sex')?{ ????????target[key]?=?value; ??????} ????} ??}); ??console.table({ ????name:?Person.name, ????sex:?Person.sex, ????age:?Person.age ??}); ??try?{ ????person.sex?=?'female'; ??}?catch(e)?{ ????console.log(e); ??}?finally{ ???? ??} }
二、進階
查看全部 -
{ ??//?es3,es5 ??function?f()?{ ????var?a?=?Array.prototype.slice.call(arguments);?//?call之后能將參數(shù)轉(zhuǎn)換成數(shù)組 ????a.forEach(function(item)?{ ??????sum?+=?item?*?1;?//?能將字符串類型轉(zhuǎn)換成數(shù)值類型 ????}) ????return?sum; ??} ??console.log(f(1,?2,?3)) } { ??//?es6 ??function?f(...a)?{?//?...a?:...擴展運算符,a是一個可變長度參數(shù)的列表,而且是個數(shù)組 ????var?sum?=?0; ????a.forEach(item?=>?{ ??????sum?+=?item?*?1; ????}); ????return?sum; ??} } { ??//?es5合并數(shù)組 ??var?params?=?['hello',?true,?7]; ??var?other?=?[1,?2].concat(param); ??console.log(other); } { ??//?es6?利用擴展運算符合并數(shù)組 ??var?params?=?['hello',?true,?7]; ??var?other?=?[1,?2,?...params]; ??console.log(other); }
查看全部 -
一、es6可以用()=> {}聲明函數(shù),這樣聲明的好處:
(1)省去function,寫法簡單。
(2)this的指向有了新的意義。
二、es6方法:
let?odds?=?evens.map(v?=>?v?+?1)
三、this指向
(1)es3/es5: this的指向是該函數(shù)被調(diào)用的指向。
var?factory?=?function()?{ ??this.a?=?'a'; ??this.b?=?'b'; ??this.c?=?{ ????a:?'a+', ????b:?function()?{ ??????return?this.a ????} ??} } console.log(new?factory().c.b());
new factory(),c.b()中,b()是c調(diào)用的,所以this指向c,得到的是a+。
(2)箭頭函數(shù)中this的指向是定義時this的指向。
b在定義函數(shù)時,this指向這個函數(shù)體中的this,函數(shù)體中的this指向factory這個構(gòu)造函數(shù)的實例。this指向factory的實例,所以this.a 就是'a'。
查看全部 -
一、常量寫法:
(1)es5
Object.defineProperty(window, 'PI2', {
????value: 3.1415926,
????writable:false
}
(2)es6
const PI = 3.1415926
查看全部 -
git clone https:///github.com/cucygh/es6-webpack.git
查看全部 -
一、callbacks中的return i*2,是對變量的引用,而不是對函數(shù)變量值的引用。函數(shù)體中是一個變量,而不是一個值。
二、用let聲明的變量有塊作用域的概念,這個時候的閉包取決于當前的塊作用域,會將當前值保存,供后面的閉包使用。
三、es6:{}表示一個新的作用域,{}可以對作用域進行隔離。
es5中要執(zhí)行立即執(zhí)行函數(shù)((function(){},()),才能對作用域進行隔離。
查看全部
舉報