SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    to get array name that is passed to a java script function

    hi all,

    I want to get the name of the array(not elements) that is passed to a function.

    So i can do operations based on the array name.

    Thank U

  2. #2
    Winemaster bronze trophy BonRouge's Avatar
    Join Date
    Oct 2004
    Location
    Sendai, Japan
    Posts
    2,417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you send the array name as part of the array? (Just an idea).

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An Array doesn't have a name. The variable you think of as the Array's name is just a transient handle that points to the Array. It isn't a string, it's string value is the string value of the Array.

    If you need a string, make the Array a property of some object and use the property name.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    hi,

    Thank u for ur ideas.

    I prefer to pass the array name along with values.

  5. #5
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Arrays don't have names, as mrhoo said, so you can't 'pass the array name'. You can call it whatever you want in the function that you are passing it to.

  6. #6
    SitePoint Member
    Join Date
    Jun 2004
    Location
    Malmö, Sweden
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should look into using JSON instead then ... then you can have a name for your object and each of the values inside it ... like this

    function myFunc( jsonobject ){
    myObject = eval ( jsonobject )
    alert( myObject.name )
    }

    www.json.org

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why the use of eval?

    Simply pass an Object with 2 properties, name and data (or whatever you wanna call it)
    Code:
    function myFunction(oMyObject)
    {
         alert(oMyObject.name); // animalObject
    
         alert(oMyObject.name + " has " + oMyObject.data.length + " animals");
    }
    
    var oMyAnimalObject = {
        name: "animalObject",
        data: [
            "dog",
            "cat",
            "horse",
            "pig",
            "bird"
       ]
    };
    
    myFunction(oMyAnimalObject);

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,817
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    That is a perfectly valid Javascript notation that predates its being borrowsd for use with JSON.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Felgall, I didn't quite get to whom your answer was to.


Bookmarks

Posting Permissions

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