On 2012-09-17 22:56, Gene Wirchenko wrote:
> On Mon, 17 Sep 2012 19:23:01 +0200, Christoph Becker
> <
cmbec...@gmx.de> wrote:
>>function Counter() {
>> var theValue = 0;
>> return {
>> getVal: function() {
> ^^^^^^^^^^^^^^^^^^
> Could you please give me pointer to where this syntax is
> discussed? I tried the standard, but what I found -- sections 12.1
> Block and 12.12 Labelled Statements -- is remarkably opaque. Do I
> even have the correct section(s)?
No, this has nothing to do with labels. The return value in this case is
an object, written in the form of an object literal. I'm sure you've
seen them before:
var obj = {
foo: 42,
bar: "baz"
};
Instead of the values 42 and "baz", you can use any valid expression,
including function expressions:
var obj = {
foo: 42,
bar: function () { return "baz"; }
};
Now you can call obj.bar() to get "baz" returned.
This is equivalent to writing -
function myFunc () {
return "baz";
}
var obj = {
foo: 42,
bar: myFunc
};
- except that in this case, myFunc could be called directly as well.
Putting it all together, Christoph's example did something like this:
return {
foo: 42,
bar: function () { return "baz"; }
};
HTH.
- stefan