closures javascript
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
add()()body {
font-family: Helvetica, Arial, sans-serif;
font-size: 12px;
}
h1 {
font-size: 1.5em;
}
h2 {
font-size: 1.2em;
}
var makeCounter = function() {
var privateCounter = 0;
function changeBy(val) {
privateCounter += val;
}
return {
increment: function() {
changeBy(1);
},
decrement: function() {
changeBy(-1);
},
value: function() {
return privateCounter;
}
}
};
var counter1 = makeCounter();
var counter2 = makeCounter();
alert(counter1.value()); /* Alerts 0 */
counter1.increment();
counter1.increment();
alert(counter1.value()); /* Alerts 2 */
counter1.decrement();
alert(counter1.value()); /* Alerts 1 */
alert(counter2.value()); /* Alerts 0 */
Also in JavaScript:
- Title
- array javascript
- Category
- JavaScript
- Title
- how to implement cland format into js file
- Category
- JavaScript
- Title
- create react app theme_color
- Category
- JavaScript
- Title
- get today's date javascript
- Category
- JavaScript
- Title
- counter exapmle in vue js
- Category
- JavaScript
- Title
- check if string is datestring javascript
- Category
- JavaScript
- Title
- build filter js
- Category
- JavaScript
- Title
- if you run a script.js with the code, how do you access the value passed to "var" inside script.js ...
- Category
- JavaScript
- Title
- discord.js multiple embeds
- Category
- JavaScript
- Title
- how to get last item in array javascript
- Category
- JavaScript
- Title
- foreach w3schools javascript
- Category
- JavaScript
- Title
- how to paste in input field react native
- Category
- JavaScript
- Title
- check palindrome js
- Category
- JavaScript
- Title
- go to anchor jquery
- Category
- JavaScript
- Title
- colors in node js console
- Category
- JavaScript
- Title
- fs create or edit file
- Category
- JavaScript
- Title
- integer to array javascript
- Category
- JavaScript
- Title
- array spread operator in javascript
- Category
- JavaScript
- Title
- if clicked outside of div jquery
- Category
- JavaScript
- Title
- adding delay in javascript foreach loop
- Category
- JavaScript
- Title
- How to get the path to the file that required your module?
- Category
- JavaScript
- Title
- angular @Output()
- Category
- JavaScript
- Title
- how to have your discord bot send two message
- Category
- JavaScript
- Title
- how to remove session data on tab close in redux\
- Category
- JavaScript
- Title
- How do you call a functional component in React
- Category
- JavaScript
- Title
- .classList
- Category
- JavaScript
- Title
- is object js
- Category
- JavaScript
- Title
- faker js
- Category
- JavaScript
- Title
- event target javascript
- Category
- JavaScript
- Title
- emmet jsx visual studio
- Category
- JavaScript
- Title
- error Unexpected end of JSON input while parsing near '...
- Category
- JavaScript
- Title
- how to deploy react app in tomcat server
- Category
- JavaScript
- Title
- compare string camelcase and lowercase javascript
- Category
- JavaScript
- Title
- check if string contains substring javascript
- Category
- JavaScript
- Title
- how to import npm packages in node.js?
- Category
- JavaScript
- Title
- expo app.json
- Category
- JavaScript
- Title
- how to get the last element in javascript
- Category
- JavaScript
- Title
- express server replit
- Category
- JavaScript
- Title
- how to send a command in js
- Category
- JavaScript
- Title
- how to Write a program that simulates a coin toss using random method of Javascript Math class
- Category
- JavaScript
- Title
- getelementbyid
- Category
- JavaScript
- Title
- how to assign same value of a key to 2 different keys in output JSON jolt
- Category
- JavaScript
- Title
- get client timezone offset JavaScript
- Category
- JavaScript
- Title
- include jquery
- Category
- JavaScript
- Title
- devexpress dxdatagrid custom text field link
- Category
- JavaScript
- Title
- in javascript check is is an array or not
- Category
- JavaScript
- Title
- chai js
- Category
- JavaScript
- Title
- how to make text come up with javascript
- Category
- JavaScript
- Title
- add item to list javascript
- Category
- JavaScript
- Title
- best react native animation library
- Category
- JavaScript
- Title
- include gif in react
- Category
- JavaScript
- Title
- export apk react native
- Category
- JavaScript
- Title
- enzyme check state
- Category
- JavaScript
- Title
- express get url parameters
- Category
- JavaScript
- Title
- add custom marker in react-mapbox-gl
- Category
- JavaScript
- Title
- how to loop through array of numbers in javascript
- Category
- JavaScript
- Title
- Error: Can't resolve 'fs' webpack
- Category
- JavaScript
- Title
- Angular watching for changes in $http.pendingRequests from directive
- Category
- JavaScript
- Title
- change select value jquery
- Category
- JavaScript
- Title
- function prototype javascript
- Category
- JavaScript
- Title
- how to make a factorial function in javascript
- Category
- JavaScript
- Title
- get last item in array js
- Category
- JavaScript
- Title
- how to get the value in a tag in react
- Category
- JavaScript
- Title
- count occurrences of character in string javascript
- Category
- JavaScript
- Title
- comparing arrays javascript
- Category
- JavaScript
- Title
- bracket notation javascript
- Category
- JavaScript
- Title
- console.log('Hi there!');
- Category
- JavaScript
- Title
- how to debug javascript
- Category
- JavaScript
- Title
- get current directory vbscript
- Category
- JavaScript
- Title
- how to find remainder in javascript
- Category
- JavaScript
- Title
- javascript bigint
- Category
- JavaScript
- Title
- how to get the height of window in javascript
- Category
- JavaScript
- Title
- click doesn't work on ajax loaded data
- Category
- JavaScript
- Title
- convert jquery to javascript
- Category
- JavaScript
- Title
- get start of day javascript
- Category
- JavaScript
- Title
- angular local storage
- Category
- JavaScript
- Title
- 1 - - 1 in javascript
- Category
- JavaScript
- Title
- * http://sizzlejs.com/
- Category
- JavaScript
- Title
- how to run function after animation complete jquery
- Category
- JavaScript
- Title
- append img to svg d3 js
- Category
- JavaScript
- Title
- angular observable subscribe and unsubscribe
- Category
- JavaScript
- Title
- how to use for of in javascript
- Category
- JavaScript
- Title
- html how to type a long string in many lines
- Category
- JavaScript
- Title
- firebase cloud functions send email
- Category
- JavaScript
- Title
- how to deap clone an object in javascript
- Category
- JavaScript
- Title
- array find method javascript
- Category
- JavaScript
- Title
- capturar el valor de un input con jquery
- Category
- JavaScript
- Title
- dom ready js
- Category
- JavaScript
- Title
- how to use ternary operatiion in sequelize join statement
- Category
- JavaScript
- Title
- append new element for each value in array d3.js
- Category
- JavaScript
- Title
- app script append two list
- Category
- JavaScript
- Title
- how to learn javascript
- Category
- JavaScript
- Title
- iterate through list javascript
- Category
- JavaScript
- Title
- best way to filter table in angular
- Category
- JavaScript
- Title
- find highest number in array javascript
- Category
- JavaScript
- Title
- how to clear radio field in jquery
- Category
- JavaScript
- Title
- javascript .firstordefault
- Category
- JavaScript
- Title
- add set time out in jquery
- Category
- JavaScript
- Title
- greater than or equal to javascript
- Category
- JavaScript
- Title
- how to generate random character from an array js
- Category
- JavaScript