Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    237
    Thanks
    18
    Thanked 34 Times in 33 Posts

    Couple of questoins regarding JS syntax

    Hello guys,

    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

  • #2
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    237
    Thanks
    18
    Thanked 34 Times in 33 Posts
    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:
    Code:
    dog = {"color":"brown", "size":"large"};
    dog["color"]; // this gives you "brown"
    dog.color; // this also gives you "brown"
    The third one is resolved to... by example I find out that:
    Code:
    (function() {alert('here')})();
    that type of syntax will execute that function as soon as page will be loaded. It is equal to:
    Code:
    function init() {alert('here')};
    init();
    what I cannot understand why placing function in ()(); executes it?

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Quote Originally Posted by PHP6 View Post
    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.

    Quote Originally Posted by PHP6
    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:
    Code:
    function foo (x) {
      alert (x);
    }
    
    var f = foo;
    
    f ("hello");
    
    (function (x, y)) { 
      alert (x + y) ;
    }) (1, 2);
    Trinithis

  • Users who have thanked Trinithis for this post:

    PHP6 (07-30-2009)

  • #4
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by PHP6 View Post

    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).

    Quote Originally Posted by PHP6 View Post

    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.

    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})();

    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.

  • Users who have thanked fside for this post:

    PHP6 (07-30-2009)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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( ).
    Last edited by Trinithis; 07-30-2009 at 09:00 AM. Reason: fixed broken quote tag

  • #6
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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):
    Code:
    var x = 1 + (3) + 5; // 9
    
    var str = ("new Array ()");;
    
    (alert) ("hello world");
    
    ({ foo: function () {return 4; } }).foo ();
    Last edited by Trinithis; 07-30-2009 at 09:14 AM.
    Trinithis

  • #7
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by Trinithis View Post

    ({ 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?

  • #8
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    237
    Thanks
    18
    Thanked 34 Times in 33 Posts
    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?

  • #9
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Code:
    ({ 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
    Last edited by abduraooft; 07-30-2009 at 04:39 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Yeah, I like Trinithis' and FSide's comments. Hey, I *did* say it wasn't really an eval( ). <grin style="sickly"/>

  • #11
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by Old Pedant View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •