www.webdeveloper.com
Results 1 to 4 of 4

Thread: Marking global variables

  1. #1
    Join Date
    Dec 2012
    Posts
    3

    Marking global variables

    Hi guys,

    First post, of many I hope.


    I'm currently working on a script that uses global vars inside objects. I notice that in my current setup it's hard to recognize what var is global and what var is defined in the function itself.

    Are there any good practices to use to mark global vars? for instance, I've seen vars that supposedly are private marked with a _ prefix, or jQuery vars prefixed with $

    Before I start prefixing with g_ I'd like to know your thoughts.

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    The best practice is really to avoid globals as much as possible. There should usually not be a need to have global objects as surely not every part of your scripts needs to access to them - only some parts does.

    But anyway, I think that having all your "globally" accessible objects inside one global object would be the best way to go, just like for example jQuery that creates the global window.jQuery object (instead of making 100 globals starting with g_).

    Otherwise you could just use the window reference when accessing globals. E.g. write 'window.myGlobal' instead of just 'myGlobal'. That way you know for sure that it's a global variable.

  3. #3
    Join Date
    Dec 2012
    Posts
    3
    actually I'm talking about a more refined method of globals, not sure if it's really called a global.

    I guess I mean the globals withing a class, so the vars that all the class functions have access to.

    Coming from PHP these vars are often referenced through self::$var which pretty much clarifies that the var is accessible in the entire class. I don't have this recognition with javascript at this time.

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by CrackyD View Post
    actually I'm talking about a more refined method of globals, not sure if it's really called a global.

    I guess I mean the globals withing a class, so the vars that all the class functions have access to.

    Coming from PHP these vars are often referenced through self::$var which pretty much clarifies that the var is accessible in the entire class. I don't have this recognition with javascript at this time.
    vars are not iterated in js. in a class, you typically use this.method() to refer to a property/method of the instance from within the constructor or a prototype method.

    you can also use the name of the constructor to create static variables that everything both inside and out can see:
    eg String.fromCharCode or Object.keys() versus ({}).keys(), which doesn't exist...

    methods and properties that are meant to be hidden are prefixed with "_", meaning, internal, don't use.
    You can actually completely hide these properties using Object.defineProperty, so that convention is not needed, but it still is fairly dominant as far as special char prefixes go.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles