I have seen javascript code that uses variables as follows:
var defaultVar = { type : 'reports', order : 'ASC', column : '1' }
What kind of variables are these? When should it be used? Also, how do you access each element?
I have seen javascript code that uses variables as follows:
var defaultVar = { type : 'reports', order : 'ASC', column : '1' }
What kind of variables are these? When should it be used? Also, how do you access each element?
That is an object literal. Used like this it acts much like a dict, hash or associative array in other languages.
You should use it wherever you need to combine several variables or functions into a single entity.
You access the members like this:
defaultVar['type'] // -> 'reports'
defaultVar.type // -> 'reports'
defaultVar['order'] = "DESC"
defaultVar.order = "DESC"
This is object in javascript. Currently its having type, order, column as its properties.
There are verious ways of creating object
Method 1: var obj = {name: 'robin', rollnumber : '1'};
Method 2 : var obj = new Object(); obj.name = 'robin'; obj.rollnumber = '1';
etc.
Objects can have methods as well. e.g. var obj = {callMe : function () {//dosomthing}};
To access elements of objects, just use dot (.) operator.
Those are JSON Objects that can be accessed like so:
var type = defaultVar.type;
This is json syntax: http://en.wikipedia.org/wiki/JSON
this is used to create an associative array.