What is the proper jargon to refer to a variable wrapped inside a function closure?
Posted
by
Rice Flour Cookies
on Programmers
See other posts from Programmers
or by Rice Flour Cookies
Published on 2012-04-04T19:39:54Z
Indexed on
2012/04/04
23:43 UTC
Read the original article
Hit count: 260
In JavaScript, there is no such thing as a "private" variable. In order to achieve encapsulation and information hiding in JavaScript, I can wrap a variable inside a function closure, like so:
var counter = (function() {
var i = 0;
var fn = {};
fn.increment = function() { i++; };
fn.get = function() { return i; };
return fn;
{)();
counter.increment();
counter.increment();
alert(counter.get()); // alerts '2'
Since I don't call i
a private variable in JavaScript, what do I call it?
© Programmers or respective owner