I have couple of simple questions regarding JS syntax, I have never seen them before.

1) What is the difference between: VAR_NAME$$constatnt and VAR_NAME? What does that $$constant word mean?

2) [RESOLVED] As far as I know that: some_var={property_one:0, property_two:0} will create an object some_var which will have two properties. I guess I will be able to access them in the following way some_var.property_two. Am I right?

3) [PARTLY RESOLVED] Finally, why sometimes some developers use function without name and how following functions will be executed or called? For example:
(function() {some operations here})();

Thanks for your replays

PHP6

07-29-2009, 03:16 PM

Ok, second question could be marked as resolved.

You can use the object declaration literal to create objects that behave much like associative arrays in other languages:

1) What is the difference between: VAR_NAME$$constatnt and VAR_NAME? What does that $$constant word mean?

Syntactically, it means nothing. $ is a valid character in a javascript identifier. A JS identifier must begin with a $, _, or letter. Characters afterward may additionally include numbers.

what I cannot understand why placing function in ()(); executes it?

The second () executes it. The first pair is sometimes not necessary, though I always use it.

To help you understand, let's say I have a function foo. foo(arg1, arg2, ...) executes (runs) the function. Such an operation is called function application. In javascript, functions are just like any other variable. IOW, they can be assigned to variables, passed as arguments, created on the fly. It is perfectly valid to do the following:

function foo (x) {
alert (x);
}

var f = foo;

f ("hello");

(function (x, y)) {
alert (x + y) ;
}) (1, 2);

fside

07-30-2009, 07:22 AM

1) What is the difference between: VAR_NAME$$constatnt and VAR_NAME? What does that $$constant word mean?

As been mentioned, dollar sign, underbar, or a letter can begin a name in Javascript. And some libraries have used one, or more, dollar signs as function names - to save typing basically. Another, jQuery, using the dollar sign as one name for itself, for its entry point (but you can also use "jQuery"). Some might name certain objects with leading double dollar signs to indicate something to themselves. But it's nothing native to Javascript, save that a dollar sign can be the first, or only, symbol in a name for an object (or primitive - a concept borrowed from java).

2) [RESOLVED] As far as I know that: some_var={property_one:0, property_two:0} will create an object some_var which will have two properties. I guess I will be able to access them in the following way some_var.property_two. Am I right?

Overloaded use/meaning of the (curly) braces and (square) brackets. It's a shorthand. Braces following a function argument parens ( function(){ . .) or control structure like "if", "for", etc, ( if( . . ){ . . ) block in the various statements. That's your function scope for a function. It's just blocking the statements for the control structure, otherwise, without scoping. But as an expression, it's the same as writing - new Object(). And you can put your properties, or initial properties, between the braces (name/value pairs). Another shorthand are the brackets. You can use them to get array elements by position. As you point out, you can get object properties by name, particularly if it's runtime and you don't know the name beforehand. But as an expression, the brackets are a shorthand for - new Array(). And similarly, one other shorthand are two forward slashes, for a regular expression pattern.

3) [PARTLY RESOLVED] Finally, why sometimes some developers use function without name and how following functions will be executed or called? For example:
(function() {some operations here})();

Thanks for your replays

The parens around the function seem to act like - eval. I myself am not sure about this. It seems to cause Javascript to evaluate/interpret the text as a function. Maybe someone knows. But you could try leaving them off and see if it still immediately executes. The second pair is what you'd use for any function call. If you just write the function name, nothing happens. You have to include an argument, or at least the parens. And you can include arguments here.

Old Pedant

07-30-2009, 08:32 AM

Quote:
Originally Posted by PHP6 View Post
3) [PARTLY RESOLVED] Finally, why sometimes some developers use function without name and how following functions will be executed or called? For example:
(function() {some operations here})();

The parens around the function seem to act like - eval. ... But you could try leaving them off and see if it still immediately executes.

It won't. If you leave them off, all you have is a function *declaration*.

Yes, the enclosing parens work something like calling eval( ). Would be better to say they "invoke the object they enclose" and if the object is a function, then it executes. But yeah, it's probably fine to think of them as being like eval( ).

Trinithis

07-30-2009, 10:05 AM

It is not like eval, and I suggest you don't think of it that way. Think of it more like a grouping, identity function, or a nop.

It's exactly the same as with the following few snippets (highlighting the () usage in question):

var x = 1 + (3) + 5; // 9

var str = ("new Array ()");;

(alert) ("hello world");

({ foo: function () {return 4; } }).foo ();

fside

07-30-2009, 03:18 PM

({ foo: function () {return 4; } }).foo ();
[/CODE]

The first parens take the context away from WINDOW . . as it were, and here returns the unnamed object, with the anonymous function as a method named "foo". In that returned context, "foo" itself can be called. And never again can that object be used, presumeably to be deleted long before the viewer leaves the page.

But isn't an object being created, and a method within, including scope chain, variables, prototype, etc? A grouping operator, any parenthesized expression, evaluates, correct? That's not like eval because it's more directly tied to the interpreter, because a text string was not interpreted nor innerHTML?

It's an 'inside-out' evaluation that discovers an object rather than a value when complete. Outside in would have WINDOW establishing context as when the method is on the right, as here. Javascript goes left to right. And this makes jQuery chaining possible since the expression on the left 'bubbles out' the context.

And not exposing any names outside the anonymous function created in this way might be used for initial page load setup, and then forgotten. Exposing a namespace creates a closure and prevents garbage collection.

Would you agree, or do you think that's missing something?

PHP6

07-30-2009, 05:25 PM

Wow! Thanks a lot Trinithis, fside and Old Pedant that was amassing and very detailed explanation. I have no more questions ;) everything is clear now.

p.s. how can I mark that topic as resolved?

abduraooft

07-30-2009, 05:27 PM

({ foo: function () {return 4; } }).foo (); Where would we use such functions? Would you please explain that definition like "foo is a function, which returns 4 ....etc" ?

p.s. how can I mark that topic as resolved?
See the last one at http://www.codingforums.com/postguide.htm

Old Pedant

07-31-2009, 12:36 AM

Yeah, I like Trinithis' and FSide's comments. Hey, I *did* say it wasn't really an eval( ). <grin style="sickly"/>

fside

07-31-2009, 07:45 PM

Yeah, I like Trinithis' and FSide's comments. Hey, I *did* say it wasn't really an eval( ). <grin style="sickly"/>

Me, too. I also wondered that it wasn't like eval, just above, or at least the parser/interpreter. But as I understand it, eval always takes a string and tries to find objects, as it were. It tries to find references. But this parenthetical alone seems to take a 'quickie' approach, separating the content into string or object. It returns any string as a string. No getting around it. It returns function as a function object. Now if it were a string, eval would parse a function, or if a string of an object name, eval would parse the entire chain. But not so just surrounding the same string with parens. So it's an obvious difference.

I guess the parens are sort of like setting up a return for the function. If you put the same function in another function and use return, or just use an alert call, you don't need the leading parens.