Created
June 20, 2020 05:31
-
-
Save joalbertg/095101110346014a2ea9780ce52fcbce to your computer and use it in GitHub Desktop.
javascript: closures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 1-basic | |
| console.log('++++++++++++++++++++++++++++++++++++++++++++++++++'); | |
| const count = (() => { | |
| let currentValue = 0; | |
| return () => currentValue += 1; | |
| })(); | |
| console.log('count:', count()); //1 | |
| console.log('count:', count()); //2 | |
| console.log('count:', count()); //3 | |
| console.log('count:', count()); //4 | |
| // 2-basic | |
| console.log('++++++++++++++++++++++++++++++++++++++++++++++++++'); | |
| const outsite = () => { | |
| let num = 0; | |
| //return () => ++num; | |
| return () => num++; | |
| } | |
| const increase = outsite(); | |
| console.log('outsite', outsite()()); //0 | |
| console.log('increase:', increase()); //0 | |
| console.log('increase:', increase()); //1 | |
| console.log('increase:', increase()); //2 | |
| console.log('increase:', increase()); //3 | |
| console.log('outsite', outsite()()); //0 | |
| // 3-basic | |
| console.log('++++++++++++++++++++++++++++++++++++++++++++++++++'); | |
| const outsite_2 = a => b => a += b; | |
| console.log('outsite_2:', outsite_2(1)(3)); //4 | |
| console.log('outsite_2:', outsite_2(10)(5));//15 | |
| const base = outsite_2(5); | |
| console.log('base:', base(1)); //6 | |
| console.log('base:', base(4)); //10 | |
| console.log('base:', base(10)); //20 | |
| console.log('base:', base(100));//120 | |
| // 2-fetch | |
| console.log('++++++++++++++++++++++++++++++++++++++++++++++++++'); | |
| require('isomorphic-fetch'); | |
| // npm i -S isomorphic-fetch | |
| // npm closures.js | |
| // rm -rf node_modules package-lock.json | |
| const crudder = domain => resource => { | |
| const url = `${domain}/${resource}`; | |
| return ({ | |
| create: (x) => fetch(url, { | |
| method: 'POST', | |
| body: JSON.stringify(x) | |
| }).then(resp => resp.json()), | |
| get: () => fetch(url).then(resp => resp.json()) | |
| }); | |
| } | |
| const baseUrl = crudder('https://jsonplaceholder.typicode.com'); | |
| const all = baseUrl('todos'); | |
| const users = baseUrl('users'); | |
| const post = baseUrl('posts'); | |
| all.get().then(resp => console.log('all:', resp[0])); | |
| users.get().then(resp => console.log('users:', resp[0])); | |
| post.create({ | |
| userId: 1, | |
| title: 'My Love', | |
| body: 'Dolor quidem ut rerum dolor?' | |
| }).then(console.log); | |
| //output | |
| //++++++++++++++++++++++++++++++++++++++++++++++++++ | |
| //count: 1 | |
| //count: 2 | |
| //count: 3 | |
| //count: 4 | |
| //++++++++++++++++++++++++++++++++++++++++++++++++++ | |
| //outsite 0 | |
| //increase: 0 | |
| //increase: 1 | |
| //increase: 2 | |
| //increase: 3 | |
| //outsite 0 | |
| //++++++++++++++++++++++++++++++++++++++++++++++++++ | |
| //outsite_2: 4 | |
| //outsite_2: 15 | |
| //base: 6 | |
| //base: 10 | |
| //base: 20 | |
| //base: 120 | |
| //++++++++++++++++++++++++++++++++++++++++++++++++++ | |
| //users: { | |
| // id: 1, | |
| // name: 'Leanne Graham', | |
| // username: 'Bret', | |
| // email: '[email protected]', | |
| // address: { | |
| // street: 'Kulas Light', | |
| // suite: 'Apt. 556', | |
| // city: 'Gwenborough', | |
| // zipcode: '92998-3874', | |
| // geo: { lat: '-37.3159', lng: '81.1496' } | |
| // }, | |
| // phone: '1-770-736-8031 x56442', | |
| // website: 'hildegard.org', | |
| // company: { | |
| // name: 'Romaguera-Crona', | |
| // catchPhrase: 'Multi-layered client-server neural-net', | |
| // bs: 'harness real-time e-markets' | |
| // } | |
| //} | |
| //all: { userId: 1, id: 1, title: 'delectus aut autem', completed: false } | |
| //{ id: 101 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment