I am new to JS and encountered several new thins. For example I saw this piece of code:
function baz() {
var x = 1;
return {
foo: function foo() {return ++x},
bar: function foo() {return --x},
};
}
var closures = baz();
alert(closures.foo(),closures.bar());
It is supposed to show a shared variable. My question is – what is being returned?
Is it simply an object with two functions foo() and bar()?
Thanks
Yotam
Yes, it’s “simply an object with two functions foo() and bar()”.
But this function is also declared in a closure, which means it can have this private property
xwhichThe main differences with
is that