object literal syntex .
Posted: Tue Nov 04, 2014 12:58 pm
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 .
now i understand this :
But what is this :
what is this alien syntex called and how wld u access its peoperties ??
Thanks .
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}
],
Thanks .