You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Although we could use code comments or JSDocs to explain what each line in this code is doing, we should rather think of leveraging the power of a functional programming language.
本文是 3 步走,看如何把下面这个千层饼嵌套的代码修改为函数式编程风格的代码
1. 原版
Click me!
varfetch=require('node-fetch');// if using NodeJSfunctionarticles(){vararr=[];returnfetch('https://dev.to/api/articles').then(function(a){if(a){returna.json().then(function(b){if(b){b.forEach((c)=>{if(c.tag_list.indexOf('javascript')!==-1&&c.tag_list.indexOf('node')!==-1){arr.push(c);}});returnarr;}});}});}articles().then(function(d){console.log(d);});
// ====== 修改前,世界模糊 ======varfetch=require('node-fetch');// if using NodeJSfunctionarticles(){vararr=[];returnfetch('https://dev.to/api/articles').then(function(a){if(a){returna.json().then(function(b){if(b){b.forEach((c)=>{if(c.tag_list.indexOf('javascript')!==-1&&c.tag_list.indexOf('node')!==-1){arr.push(c);}});returnarr;}});}});}articles().then(function(d){console.log(d);});
写更简洁的代码 by 函数式编程
本文是 3 步走,看如何把下面这个千层饼嵌套的代码修改为函数式编程风格的代码
1. 原版
Click me!
2. 改版
Click me again!
但是下面这个版本才是最终形态
3. 最终版
Click me again and again!
天,这真的是很优雅了
怎么样来理解呢,举个例子,我们看
byTags()
这个函数,变化为ES5
就是目的是:传入一个 tags 变量,被新返回的函数
function(article)
使用。而这个新返回的函数的 parameter 是从filter
函数的参数的第一个参数(对不起,就是这样的绕)articles.filter(byTags(tagsToFilter));
,所以就是 articles 中的每个 article最后,再放一次看看修改前后的差别吧
The text was updated successfully, but these errors were encountered: