www.webdeveloper.com
Results 1 to 9 of 9

Thread: Custom variable javascript

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Custom variable javascript

    Hello there, I am trying to create a new global variable in a function with a custom name.

    I need something like this:

    function newVariable(name){
    createVariableWithName(name);
    }

  2. #2
    Join Date
    Oct 2011
    Location
    India
    Posts
    16
    I don't think that is possible. But you can create something like this.

    var glb = new Object();
    function newVariable(name, val){
    glb.name = val;
    }

    You can thereafter refer to this variable using glb.name

  3. #3
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,774

  4. #4
    Join Date
    Aug 2007
    Posts
    3,767
    Quote Originally Posted by krismvk1998 View Post
    var glb = new Object();
    function newVariable(name, val){
    glb.name = val;
    }
    Won't work as expected, you'll need to use glb[name] = val; And as toicontien pointed out, you can extend that to window[name] = val, but I wouldn't recommend either using that many global variables that you need a function to define them, or using a function to define them unless you've a very good reason.
    Last edited by Declan1991; 10-09-2011 at 01:31 PM. Reason: Better phrasing

  5. #5
    Join Date
    Oct 2011
    Location
    India
    Posts
    16
    Hi Declan,

    glb[name] should be used if "glb" has to be used as an array.
    Here my intention is to use glb as an object and thereafter use it's custom "attributes" of glb object as variables.

    I didn't understand as to how this can give some unexpected result. Can you give an example so that I can understand this better?

    Thanks,
    Krishna

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by krismvk1998 View Post
    Can you give an example so that I can understand this better?
    Example 1

    Code:
    function newVariable(name, val){
    glb.name = val;
    }
    newVariable('FirstName','Joe')
    Will set to the object a property called name, with the value Joe
    While in
    Example 2
    Code:
    function newVariable(name, val){
    glb[name] = val;
    }
    newVariable('FirstName','Joe');
    Will set a property called FirstName, with the value Joe

    In the first example, in newVariable(name) it is an argument passed to a function, while in glb.name it is something different: a new property of the object. No relationship between the those two name there

    Quote Originally Posted by krismvk1998 View Post
    glb[name] should be used if "glb" has to be used as an array.
    No.
    Last edited by Kor; 10-11-2011 at 05:03 AM.

  7. #7
    Join Date
    Aug 2007
    Posts
    3,767
    Kor has a great explanation there, but just to add a little to it: using the following syntax,
    window["document"]["getElementById"](id)
    has nothing to do with arrays, although it looks similar. Arrays are 0-index and the keys must be numeric. In that sense, there is no such thing as an associative array in JavaScript, merely numeric arrays and objects with properties.

  8. #8
    Join Date
    Oct 2011
    Location
    India
    Posts
    16
    Thanks Kor & Declan for the examples. I thought that I knew javascript. I think I'll have to start looking at the basics now

  9. #9
    Join Date
    Oct 2011
    Location
    India
    Posts
    16
    I was reading about associative arrays in javascript now. Now I completely understand what you've wrote above there Kor.

    Wow.. what an explanation. Hats off to you Kor.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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