[Functional Programming] Create Reusable Functions with Partial Application in JavaScript
JavaScript in with create application Programming functional Functions
2023-09-14 08:59:14 时间
This lesson teaches you how arguments passed to a curried function allow us to store data in closure to be reused in our programs and applications. Since each argument, except for the final one, returns a new function, we can easily create reusable functions by supplying some of these arguments beforehand, and sharing these partially applied functions with other parts of our codebase. In this lesson, we'll create a curried function to fetch requests from an API that uses partial application to create reusable functionality.
Another way to do partially API call is using Proxy. Check this out.
// Partial Application // Curried functions create a wonderful emergent property, "partial application", // that is useful for building up reusability in our applications that you // just can't get with normal, multivariate functions. Because curried functions // return a new function with each argument (except for the final one), we are // able to "partially apply" values and store them in closure, available to any // subsequent function, thus creating new, reusable functions with some values // already supplied. // Imagine we have an application that needs to make requests to different APIs. // We can create a function that bakes in the base URL, while allowing other // arguments to be passed in later const fetch = require('node-fetch') const getFromAPI = baseURL => endPoint => callback => fetch(`${baseURL}${endPoint}`) .then(res => res.json()) .then(data => callback(data)) .catch(err => { console.error(err.message) }) // Now we can partially apply a baseURL to create a reusable function for // one of our APIs const getGithub = getFromAPI( 'https://api.github.com' ) // We can create several get request functions by partially applying different // endpoints to our getGithub function const getGithubUsers = getGithub('/users') const getGithubRepos = getGithub('/repositories') // Now we can use our callback to get the data and do something with it. getGithubUsers(data => data.forEach(user => { console.log(`User: ${user.login}`) }) ) getGithubRepos(data => data.forEach(repo => { console.log(`Repo: ${repo.name}`) }) ) // We can still continue to reuse previous partially applied functions const getGithubOrgs = getGithub('/organizations') getGithubOrgs(data => data.forEach(org => { console.log(`Org: ${org.login}`) }) ) // We can start the process all over by partially applying a new baseURL const getReddit = getFromAPI('https://reddit.com') // And let's get some pictures of some cute animals const getRedditAww = getReddit('/r/aww.json') // And fetch the URLs of those images const imageURLs = getRedditAww(payload => payload.data.children.forEach(child => { console.log( child.data.preview.images[0].source.url ) }) )
相关文章
- 记录一次JavaScript正则诡异经历
- JS转换HTML转义符,防止javascript注入攻击,亲测可用「建议收藏」
- 【说站】JavaScript for-in和for-of的不同点
- 版本号的正则表达式-身份证号码的正则表达式及验证详解(JavaScript,Regex)
- 20个稀奇古怪的 JavaScript 表达式
- 2017年 JavaScript 框架回顾 — 后端框架详解编程语言
- javascript 正则表达式详解编程语言
- MySQL中实现嵌套查询的IN关键字用法(mysql嵌套查询in)
- MySQL中的IN运算符技巧(mysql查in)
- 提升Oracle中IN操作的效率(oracle的in效率)
- MSSQL条件查询IN机制优化技巧(mssql条件查询in)
- MySQL中IN运算符的使用技巧(mysql中 与in)
- MySQL中IN的用法和意义(mysql中in的意思)
- MySQL中IN操作最大长度详解(mysql中in最大长度)
- MySQL中IN语句的参数化使用方法(mysql中in参数化)
- 深入理解Oracle中IN运算符的用法(oracle中的in用法)
- 条件在Oracle中使用IN和OR查询条件的巧妙之处(oracle中in跟or)
- 的影响Oracle中IN操作对索引效率的影响研究(oracle中in对索引)
- Oracle IN一种SQL语句查询的高效方式(oracle in的定义)
- JavaScript初级教程(第二课)
- 用javascript实现图片马赛克后显示并切换
- javascript下for(in)语句获得所有style的【scrollbar】滚动条样式内容
- 在JavaScript中调用php程序
- 几个javascript操作word的参考代码
- javascript设置某DIV区域内的checkbox复选框
- javascript函数中的arguments参数
- 通过JavaScript控制字体大小的代码
- JavaScript表达式:URL协议介绍
- 你必须知道的Javascript知识点之"this指针"的应用
- JavaScript中for-in遍历方式示例介绍