欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

underscore之Chaining_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

 更新時(shí)間:2017年07月10日 10:10:08   作者:liaoxuefeng  
本文通過文字說明與代碼的形式給大家介紹了underscore之Chaining的相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧

還記得jQuery支持鏈?zhǔn)秸{(diào)用嗎?

$('a').attr('target', '_blank')
   .append(' <i class="uk-icon-external-link"></i>')
   .click(function () {});

如果我們有一組操作,用underscore提供的函數(shù),寫出來像這樣:

_.filter(_.map([1, 4, 9, 16, 25], Math.sqrt), x => x % 2 === 1);
// [1, 3, 5]

能不能寫成鏈?zhǔn)秸{(diào)用?

能!

underscore提供了把對(duì)象包裝成能進(jìn)行鏈?zhǔn)秸{(diào)用的方法,就是chain()函數(shù):

_.chain([1, 4, 9, 16, 25])
 .map(Math.sqrt)
 .filter(x => x % 2 === 1)
 .value();
// [1, 3, 5]

因?yàn)槊恳徊椒祷氐亩际前b對(duì)象,所以最后一步的結(jié)果需要調(diào)用value()獲得最終結(jié)果。

小結(jié)

通過學(xué)習(xí)underscore,是不是對(duì)JavaScript的函數(shù)式編程又有了進(jìn)一步的認(rèn)識(shí)?

相關(guān)文章

最新評(píng)論