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 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    75
    Thanks
    2
    Thanked 0 Times in 0 Posts

    is this an object object, or an array, or what ?

    this code was written for me by someone who knows what he is doing. i do not.
    i am trying to understand the format of the variable 'manifest' below (by using alerts). but my approaches all fail.
    are a & b also variables ?
    how can i describe in code that i want to see the value of say, 'filename' ?

    no doubt it is a simple concept, but i have been trying for weeks to sort it out.
    will someone point my in the right direction ?



    Code:
    <script>
    
    $(document).ready(function() 
    {
    
    var manifest = 
    
          a:{
    		filename:"01.jpg",
    		aphorism: "old"		
    		},
            
    	b:{
    		item:"Item# 260",
    		epigram: "A Wise Man"
    		
    		}
    
            alert(manifest);
            alert(a);
            alert(filename);
    
    });
    
    </script>
    Last edited by pratto; 07-07-2013 at 08:05 AM.

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    If that is 1:1 the code that person gave you, I doubt that they "know what they're doing". The code is not even correct and will throw an error. If I had to guess what it should look like, I would say this:

    Code:
    var manifest = {
        a: {
            filename : "01.jpg",
            aphorism : "old"		
        },
    
        b: {
            item : "Item# 260",
            epigram : "A Wise Man"
        }
    };
    Now, manifest is an object with the keys a and b of which both are objects again with different keys. So no, a and b are not variables. To access filename, you would write

    Code:
    manifest.a.filename
    or

    Code:
    manifest['a']['filename']
    The first is the actual object notation, but the second will work just as well and that is what you would do, for example, if the 'a' was some input by a user.

  • #3
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    Use the development tools (console) of your browser to test this code (without jQuery which is not necessary for this simple test)
    Code:
    var manifest = 
          a:{
    		filename:"01.jpg",
    		aphorism: "old"		
    		},
    	b:{
    		item:"Item# 260",
    		epigram: "A Wise Man"
    		}
            alert(manifest);
            alert(a);
            alert(filename);
    This code is wrong, manifest is not define !
    You have to insert brackets to define an object manifest
    Code:
    var manifest={
    	a:{
    		filename:"01.jpg",
    		aphorism: "old"		
    		},
    	b:{
    		item:"Item# 260",
    		epigram: "A Wise Man"
    		}
    };
           alert(manifest); // an object
            alert(manifest.a);// an object
            alert(manifest.a.filename);// a value
            alert(JSON.stringify(manifest));// a method to read the whole object
    Last edited by 007julien; 07-07-2013 at 07:01 PM.

  • #4
    New Coder
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    75
    Thanks
    2
    Thanked 0 Times in 0 Posts
    i thank you both very much. that was a big help.
    i had never heard the term 'key' before.

    as far as the original code goes, it works beautifully in my website. i must not have made a faithful distillation of it when i brought it in. that was what was aggravating me. i used the code to partially drive my website, but could not understand what it meant. aggravating.

    ok. i will make a fresh start at understanding it. braces, keys, colons. hmmm

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,027
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I mildly disagree with Airblader about his use of the term "keys".

    Technically they are properties of the object. Or, even more correctly, property names, since the actual properties are the values associated with each property name.

    There is absolutely nothing wrong with thinking of them as "keys", of course. And it does match the usage of 'keys" and "values" as seen in other languages (e.g., PHP).

    But I find it much more intuitive to think of them as properties, with property names, because then it more closely matches the DOM with its properties, property names, and more. And, too, the multiple levels matches more closely, too.

    Example:
    Code:
    <input id="foo" style="color: red;" />
    ...
    var inp = document.getElementById("foo"); // get an object reference
    alert( inp.style.color ); // get the property style and then the sub-property color
    See how similar it is?

    But by all means, if you are used to key=>value pairs, as in PHP, go ahead and think of them that way.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Thanks, Old Pedant. I guess my old PHP days came through. As you said, either way is fine, but having some perspective never hurts.

  • #7
    New Coder
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    75
    Thanks
    2
    Thanked 0 Times in 0 Posts
    right. thanks.

    i did find this from mozilla that discusses objects and keys in ecmascript.

    https://developer.mozilla.org/en-US/...ts/Object/keys
    Last edited by pratto; 07-08-2013 at 04:06 AM. Reason: found helpful link


  •  

    Posting Permissions

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