closure javascript
JavaScript
var 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()()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)); // 12function makeFunc() {
var name = 'Mozilla';
function displayName() {
alert(name);
}
return displayName;
}
var myFunc = makeFunc();
myFunc();
body {
font-family: Helvetica, Arial, sans-serif;
font-size: 12px;
}
h1 {
font-size: 1.5em;
}
h2 {
font-size: 1.2em;
}
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
Also in JavaScript:
- Title
- javascript check if a value is an int
- Category
- JavaScript
- Title
- how to code print in javascript
- Category
- JavaScript
- Title
- iso to date javascript
- Category
- JavaScript
- Title
- array to set javascript
- Category
- JavaScript
- Title
- how to remove an object from array in react native
- Category
- JavaScript
- Title
- how to define variable in javascript
- Category
- JavaScript
- Title
- index and id togtgher angularjs
- Category
- JavaScript
- Title
- copy one array to another javascript
- Category
- JavaScript
- Title
- how to know which button clicked in javascript
- Category
- JavaScript
- Title
- how to use react memo hooks
- Category
- JavaScript
- Title
- indexing string in javascript
- Category
- JavaScript
- Title
- angular transition animation
- Category
- JavaScript
- Title
- getcollectionnames
- Category
- JavaScript
- Title
- history.pushstate
- Category
- JavaScript
- Title
- codemirror get object from textarea
- Category
- JavaScript
- Title
- can we add new state property using setstate in react
- Category
- JavaScript
- Title
- how to write in js
- Category
- JavaScript
- Title
- comment in react
- Category
- JavaScript
- Title
- generate random numbers in js
- Category
- JavaScript
- Title
- export apk react native
- Category
- JavaScript
- Title
- .includes javascript
- Category
- JavaScript
- Title
- how to reload vscode
- Category
- JavaScript
- Title
- how to get last item in array javascript
- Category
- JavaScript
- Title
- fixed header on scroll vuejs
- Category
- JavaScript
- Title
- access to static file nodejs
- Category
- JavaScript
- Title
- check unique object in array javascript site:stackoverflow.com
- Category
- JavaScript
- Title
- Getting the differences between two objects javascript lib
- Category
- JavaScript
- Title
- inheritance in javascript
- Category
- JavaScript
- Title
- accèder data-id javascript
- Category
- JavaScript
- Title
- expressjs query params
- Category
- JavaScript
- Title
- how to add text to h2 with jquery
- Category
- JavaScript
- Title
- for of and for in javascript
- Category
- JavaScript
- Title
- butonc click jquery
- Category
- JavaScript
- Title
- how to remove a property from an object in javascript
- Category
- JavaScript
- Title
- javascript change attribute
- Category
- JavaScript
- Title
- axios post with header
- Category
- JavaScript
- Title
- binary gap
- Category
- JavaScript
- Title
- javascript add scroll listener
- Category
- JavaScript
- Title
- component navigation without changin the url react router
- Category
- JavaScript
- Title
- fivem trigger javascript function
- Category
- JavaScript
- Title
- enable disable click on div jquery
- Category
- JavaScript
- Title
- ionic modal navbar not showing
- Category
- JavaScript
- Title
- for javascript delay
- Category
- JavaScript
- Title
- how to check if a number is even or odd in javascript
- Category
- JavaScript
- Title
- get date javascript
- Category
- JavaScript
- Title
- how to delay redirect in javascript
- Category
- JavaScript
- Title
- how to verify timestamp format in javascript
- Category
- JavaScript
- Title
- how to access router from the store vue
- Category
- JavaScript
- Title
- default props react
- Category
- JavaScript
- Title
- button style not working react native
- Category
- JavaScript
- Title
- javascript array concat spread operator
- Category
- JavaScript
- Title
- how to output to console java
- Category
- JavaScript
- Title
- javascript check if is nan
- Category
- JavaScript
- Title
- empty textarea using jquery
- Category
- JavaScript
- Title
- disable mouse right click javascript
- Category
- JavaScript
- Title
- find the last occurrence of a character in a string javascript
- Category
- JavaScript
- Title
- how to trigger an event on reload js
- Category
- JavaScript
- Title
- how to get the end of an array javascript
- Category
- JavaScript
- Title
- how to parse json
- Category
- JavaScript
- Title
- install nodejs from ubuntu 18.04
- Category
- JavaScript
- Title
- angular get name of component
- Category
- JavaScript
- Title
- javascript click button by id
- Category
- JavaScript
- Title
- how to create array in javascript
- Category
- JavaScript
- Title
- how to make a function in javascript
- Category
- JavaScript
- Title
- 1. Write regular expression to describe a languages consist of strings made of even numbers a and b. CO1 K3
- Category
- JavaScript
- Title
- angry professor javascript
- Category
- JavaScript
- Title
- how to check how many strings are in a sentence javascript
- Category
- JavaScript
- Title
- createnodefield
- Category
- JavaScript
- Title
- airbnb and eslint react native
- Category
- JavaScript
- Title
- angular npm angular material
- Category
- JavaScript
- Title
- closure javascript
- Category
- JavaScript
- Title
- how to change color of font in js
- Category
- JavaScript
- Title
- Discord embeds
- Category
- JavaScript
- Title
- how to run cypress test
- Category
- JavaScript
- Title
- angular reactive form get form control template
- Category
- JavaScript
- Title
- how set defualt image for dropify
- Category
- JavaScript
- Title
- email regex
- Category
- JavaScript
- Title
- how To clear all the input element inside div using jquery
- Category
- JavaScript
- Title
- how to add class to button in javascript
- Category
- JavaScript
- Title
- adding bootstrap to angular global styles
- Category
- JavaScript
- Title
- https package node post request
- Category
- JavaScript
- Title
- date to string format javascript
- Category
- JavaScript
- Title
- how to concatenate strings javascript
- Category
- JavaScript
- Title
- For-each over an array in JavaScript
- Category
- JavaScript
- Title
- bootstrap 4 hide modal via javascript
- Category
- JavaScript
- Title
- how to add javascript to html
- Category
- JavaScript
- Title
- heroku failed to run files from static folder in node
- Category
- JavaScript
- Title
- how to check validation for file extension .csv in angular
- Category
- JavaScript
- Title
- clear the command prompt node
- Category
- JavaScript
- Title
- input search picture jquery
- Category
- JavaScript
- Title
- html loop through array
- Category
- JavaScript
- Title
- in javascript how to split string
- Category
- JavaScript
- Title
- angular add debounce time before putting valu in subject next
- Category
- JavaScript
- Title
- installing bootstrap in angular 9
- Category
- JavaScript
- Title
- command to start api on react
- Category
- JavaScript
- Title
- graal.js javascript array in java
- Category
- JavaScript
- Title
- create new node project
- Category
- JavaScript
- Title
- how to print NODE_PATH
- Category
- JavaScript
- Title
- caesar cipher javascript code
- Category
- JavaScript
- Title
- cordova capacitor document viewer fail
- Category
- JavaScript