I was reading an expemple of javascript closure. And there is one thing I don't understand.
Here is the exemple:
function makeSizer(size) {
return function() {
document.body.style.fontSize = size + 'px';
};
}
var size12 = makeSizer(12);
var size14 = makeSizer(14);
var size16 = makeSizer(16);
document.getElementById('size-12').onclick = size12;
document.getElementById('size-14').onclick = size14;
document.getElementById('size-16').onclick = size16;
Here is the full exemple: Fidle Exemple
What I understand, Is when you click on the element size-12 for ex, you will execute the anonymous function which change the size to 12.
What I don't understand, is if you change the code this way:
function makeSizer(size) {
document.body.style.fontSize = size + 'px';
}
and call makeSizer(12) instead of size12 on click event, it doesn't work.
Ok I get this is what make closure interesting, but why my modification does not work ?
Regard, Charles.