CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2006
    Posts
    306

    [RESOLVED] Javascript: $ Symbol Meaning

    What does "$" do exactly? I've seen it in a couple of scripts.

    Ex: $name = "rocky";
    Last edited by code?; August 11th, 2008 at 03:06 AM.

  2. #2
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Javascript: $ Symbol Meaning

    As far as I'm aware, i makes no difference, as you can start a variable name in Javascript with anything you like...

    If you've seen it in one of my scripts, it's a leftover habit because I've been doing a lot of PHP currently, and that requires that all variables begin with a $. As it (as far as I know) makes no difference, I use it to save mistakes in PHP.
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  3. #3
    Join Date
    May 2006
    Posts
    306

    Re: Javascript: $ Symbol Meaning

    Oh ok, I just googled it and sometimes it's used for regex.

    What does
    Code:
    (div.visible(element) ? 'hide' : 'show']
    do? I want to learn about that syntax.
    Last edited by code?; August 11th, 2008 at 02:53 AM.

  4. #4
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Javascript: $ Symbol Meaning

    Quote Originally Posted by code?
    What does
    Code:
    (div.lement.visible(element) ? 'hide' : 'show']
    do? I want to learn about that syntax.
    Code:
    variable = (condition ? result1 : result2);
    Is equivalent to
    Code:
    if (condition) {
      variable = result1;
    } else {
      variable = result2;
    }
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  5. #5
    Join Date
    May 2006
    Posts
    306

    Re: Javascript: $ Symbol Meaning

    Lol, one more question...

    How would I type up a static class in javascript, So I could easily write...

    Code:
    Class.Function.DoIt();
    ...without having to write...
    Code:
    var Class = new myClass();

    Also, i googled "javascript class" and dug around and found
    Code:
    // JavaScript Pet class
    
    function Pet(name) {
        this._name = name;
    }
    
    Pet.prototype._name;
    
    Pet.prototype.getName = function() {
        return this._name;
    }
    Now, could you get _name without using the getName function?

    thats about it.
    Last edited by code?; August 11th, 2008 at 03:08 AM.

  6. #6
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Javascript: $ Symbol Meaning

    What about:
    Code:
    Pet.prototype.name;
    -OR-
    Pet.prototype.name = "NO NAME";
    As for a static class, I'll throw you a really complex example - it's a UTF-8 encoder:
    Some of the syntax is wierd - I think there using one of the JavaScript variants.
    Code:
    var Url = {
        encode : function (string) {
            return escape(this._utf8_encode(string));
        },
        decode : function (string) {
            return this._utf8_decode(unescape(string));
        },
        _utf8_encode : function (string) {
          //Code removed for sanity's sake
        },
        _utf8_decode : function (utftext) {
          //Code removed for sanity's sake
        }
    }
    
    s = Url.encode(s)
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  7. #7
    Join Date
    May 2002
    Posts
    10,943

    Re: Javascript: $ Symbol Meaning

    The $ (dollar sign) is a custom function. It is not for regex, nor does it have anything to do with PHP.

    It was created a few years back to make the process of finding/getting elements much simpler. Take a look at this post from my website. It clarifies this well.

    Code:
    (statement) ? true : false;
    This is known as ternary logic. Nice and simple. It takes an if statement to one line and makes the processing much faster.

    Quote Originally Posted by code?
    How would I type up a static class in javascript, So I could easily write...
    Let's make this much simpler...try using objects. They go hand in hand with classes.

    Code:
    var myObject = {
      myFunction = function(str) {
        alert(str);
      },
    
      myRandomVariable: 'Hello world!'
    }
    
    // then to call it
    myObject.myFunction('Hello world!');
    Last edited by PeejAvery; August 11th, 2008 at 08:03 AM.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured