Create a gist now

Instantly share code, notes, and snippets.

@Kcko /closure.html
Last active May 12, 2018

Embed
What would you like to do?
<p id="demo">0</p>
<script>
var add = (function () {
var counter = 0;
return function () {return counter += 1;}
})();
function myFunction(){
document.getElementById("demo").innerHTML = add();
}
</script>
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 */
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
// https://diskuse.jakpsatweb.cz/?action=vthread&forum=8&topic=103393
for(var i=0; stylNameArray.length>i; i++) {
(function(){
var n = i;
neco[i].onclick = function() {
zmenaStylu("styl_pozadi_oken_0" + n);
}
})();
}
for(var i=0; stylNameArray.length>i; i++) {
(function(i){
neco[i].onclick = function() {
zmenaStylu("styl_pozadi_oken_0" + i);
}
})(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment