object literal syntex .

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

object literal syntex .

Post by gautamz07 »

Just found this really cool snippet , this is not really a question , i just don't understand wht "that is called" , what i mean is user here is a variable and inside user u have data .

Code: Select all

var user = {
            data        :[
                {name:"T. Woods", age:37},
                {name:"P. Mickelson", age:43}
            ],
            clickHandler:function (event) {
                var randomNum = ((Math.random () * 2 | 0) + 1) - 1; // random number between 0 and 1

                // This line is printing a random person's name and age from the data array
                console.log (this.data[randomNum].name + " " + this.data[randomNum].age);
            }

        }

        // Pass the user object's clickHandler method as a callback to the button's click method
        // The button is wrapped inside a jQuery $ wrapper, so it is now a jQuery object
        // And the output will be undefined
        $ (".buttonError").click (user.clickHandler); // undefined
    
    $ (".buttonGood").click (user.clickHandler.bind (user));

now i understand this :

Code: Select all

data  :  {
                name:"T. Woods", age:37,
                name:"P. Mickelson", age:43
}
        

But what is this :

Code: Select all

data        :[
                {name:"T. Woods", age:37},
                {name:"P. Mickelson", age:43}
            ],
what is this alien syntex called and how wld u access its peoperties ?? :)

Thanks .
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: object literal syntex .

Post by Celauran »

It's an array ([]) of objects ({})
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: object literal syntex .

Post by gautamz07 »

Thanks ..
Post Reply