javascript closure
JavaScript
function makeAdder(x) {
return function(y) {
return x + y;
};
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);
console.log(add5(2)); // 7
console.log(add10(2)); // 12var counter = (function() {
var privateCounter = 0;
function changeBy(val) {
privateCounter += val;
}
return {
increment: function() {
changeBy(1);
},
decrement: function() {
changeBy(-1);
},
value: function() {
return privateCounter;
}
};
})();
console.log(counter.value()); // logs 0
counter.increment();
counter.increment();
console.log(counter.value()); // logs 2
counter.decrement();
console.log(counter.value()); // logs 1
function bonjour(prenom) {
let resultat = 'Bonjour ' + prenom ;
let maClosure = () => console.log(resultat) ;
return maClosure ;
}
let mafonction = bonjour('Rem\'s') ;
mafonction();
function timer() {
let = secondes = 0
let maclosure = () =>{
return ++secondes ;
}
return maclosure ;
}
let montimer = timer() ;
console.log(montimer()) ;
console.log(montimer()) ;
console.log(montimer()) ;
// premier timer
let monDeuxiemeTimer = timer() ;
console.log(monDeuxiemeTimer()) ;
console.log(monDeuxiemeTimer()) ;
console.log(monDeuxiemeTimer()) ;
//deuxieme timer
console.log(montimer()) ;
//premier timer
mafonction() ;
// demo bonjour Rem's
body {
font-family: Helvetica, Arial, sans-serif;
font-size: 12px;
}
h1 {
font-size: 1.5em;
}
h2 {
font-size: 1.2em;
}
add()()
Also in JavaScript:
- Title
- how to use axios in vue
- Category
- JavaScript
- Title
- how to get timestamp in javascript of a date object
- Category
- JavaScript
- Title
- how to check how many strings are in a sentence javascript
- Category
- JavaScript
- Title
- javascript check if value in array
- Category
- JavaScript
- Title
- javascript base64 encode file input
- Category
- JavaScript
- Title
- disable mixed content via javascript
- Category
- JavaScript
- Title
- generate random ip address javascript
- Category
- JavaScript
- Title
- javascript canvas gamma correction
- Category
- JavaScript
- Title
- if statement in ejs
- Category
- JavaScript
- Title
- javascript circular evaluation
- Category
- JavaScript
- Title
- how to count the rows of gridview in asp.net using jquery
- Category
- JavaScript
- Title
- clone object in js
- Category
- JavaScript
- Title
- fetch api javascript
- Category
- JavaScript
- Title
- javascript bootstrap ontab change
- Category
- JavaScript
- Title
- how to pass a value to a react funtion without immediately firing it
- Category
- JavaScript
- Title
- javascript check if variable exists
- Category
- JavaScript
- Title
- javascrip reverse text
- Category
- JavaScript
- Title
- fetch api based on id nodejs and mongodb
- Category
- JavaScript
- Title
- javascript base64 encode string
- Category
- JavaScript
- Title
- how to create component in reactjs
- Category
- JavaScript
- Title
- add bootstrap to gatsby
- Category
- JavaScript
- Title
- array length javascript
- Category
- JavaScript
- Title
- evaluation of postfix expression using stack in c
- Category
- JavaScript
- Title
- aws amplify get JWT TOKEN
- Category
- JavaScript
- Title
- create many items from an array to canvas
- Category
- JavaScript
- Title
- javascript array split chunk
- Category
- JavaScript
- Title
- chrome add a bookmark that appends to current url
- Category
- JavaScript
- Title
- alternative for componentdidmount
- Category
- JavaScript
- Title
- how to set random dice image with js
- Category
- JavaScript
- Title
- discord.js check for permissions
- Category
- JavaScript
- Title
- add an element to an array javascript
- Category
- JavaScript
- Title
- convert an image into Base64 string using JavaScript
- Category
- JavaScript
- Title
- how to print two arrays together
- Category
- JavaScript
- Title
- get screen resolution jquery
- Category
- JavaScript
- Title
- formatDoubl js
- Category
- JavaScript
- Title
- check if checkbox is checked javascript
- Category
- JavaScript
- Title
- client timezone offset in JavaScript
- Category
- JavaScript
- Title
- Inserting HTML elements with JavaScript
- Category
- JavaScript
- Title
- javascript click event notifications
- Category
- JavaScript
- Title
- javascript check if object is empty
- Category
- JavaScript
- Title
- Discord.js Basics
- Category
- JavaScript
- Title
- firebase google yolo
- Category
- JavaScript
- Title
- how to check if item is in list js
- Category
- JavaScript
- Title
- events in javascript
- Category
- JavaScript
- Title
- dynamic properties instead javascript
- Category
- JavaScript
- Title
- array remove element js
- Category
- JavaScript
- Title
- javascript associative array
- Category
- JavaScript
- Title
- copy object array javascript
- Category
- JavaScript
- Title
- how to get the status of other urls in ajax
- Category
- JavaScript
- Title
- how to get the timestamp in javascript
- Category
- JavaScript
- Title
- javascript change element id
- Category
- JavaScript
- Title
- how to run javascript in terminal
- Category
- JavaScript
- Title
- billie eilish
- Category
- JavaScript
- Title
- javascript change frame background
- Category
- JavaScript
- Title
- javascript clear text in textarea
- Category
- JavaScript
- Title
- datetime to date javascript
- Category
- JavaScript
- Title
- express validator
- Category
- JavaScript
- Title
- convert hashmap into json
- Category
- JavaScript
- Title
- How to scan a folder for documents with javascript
- Category
- JavaScript
- Title
- assign array to another array javascript
- Category
- JavaScript
- Title
- how to make an async function
- Category
- JavaScript
- Title
- how to use jq with json data
- Category
- JavaScript
- Title
- angular list contains property
- Category
- JavaScript
- Title
- chart.js radar chart
- Category
- JavaScript
- Title
- How to get row index and cellindex together in javascript
- Category
- JavaScript
- Title
- datatables server side
- Category
- JavaScript
- Title
- clear formarray in angular 6
- Category
- JavaScript
- Title
- bubble sort
- Category
- JavaScript
- Title
- fetch get data js
- Category
- JavaScript
- Title
- convert json to 2d array
- Category
- JavaScript
- Title
- boucle foreach js
- Category
- JavaScript
- Title
- disable autocomplete javascript
- Category
- JavaScript
- Title
- codeceptjs "waitForClickable"
- Category
- JavaScript
- Title
- javascript array filter elements greater than
- Category
- JavaScript
- Title
- javascript capitalize first letter of each word
- Category
- JavaScript
- Title
- event.stoppropagation
- Category
- JavaScript
- Title
- how to add set between two date in datatable
- Category
- JavaScript
- Title
- export to logs to elasticsearch node from fluend container k8s
- Category
- JavaScript
- Title
- angular run with proxy
- Category
- JavaScript
- Title
- javascript check if undefined or null
- Category
- JavaScript
- Title
- how to write regular expression
- Category
- JavaScript
- Title
- add val to array jquery
- Category
- JavaScript
- Title
- bootstrap 4 open tab when opening modal
- Category
- JavaScript
- Title
- check node version
- Category
- JavaScript
- Title
- Javascript compare two objects
- Category
- JavaScript
- Title
- how to calculate balance automatically using javascript and php using cash paid
- Category
- JavaScript
- Title
- javascript check if not undefined
- Category
- JavaScript
- Title
- discord.js how to go back a file
- Category
- JavaScript
- Title
- how to remove 000webhost watermark 2019
- Category
- JavaScript
- Title
- ajax open new tab with post
- Category
- JavaScript
- Title
- get x, y of element js
- Category
- JavaScript
- Title
- endpoint js
- Category
- JavaScript
- Title
- check if input is a number javascript
- Category
- JavaScript
- Title
- how to return the max and min of an array in javascript
- Category
- JavaScript
- Title
- how to include local image files in javascript object
- Category
- JavaScript
- Title
- iterate array javascript
- Category
- JavaScript
- Title
- document get element by tag
- Category
- JavaScript
- Title
- how to use a js class in react
- Category
- JavaScript
- Title
- destructure array javascript
- Category
- JavaScript
- Title
- elasticsearch test node.js
- Category
- JavaScript