0

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?

Mary
  • 1,505
  • 5
  • 27
  • 44
  • It's an object literal. – gen_Eric Nov 19 '13 at 21:18
  • 1
    http://stackoverflow.com/a/11922384/1385672 – wirey00 Nov 19 '13 at 21:23
  • 1
    [A re-introduction to JavaScript (JS Tutorial)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript). Note that "jQuery variable" isn't a thing - jQuery is essentially a collection of JavaScript functions, it doesn't have its own variables or syntax separate to what JavaScript has. – nnnnnn Nov 19 '13 at 21:42

4 Answers4

3

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"
joews
  • 29,767
  • 10
  • 79
  • 91
1

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.

BSS
  • 76
  • 2
  • 2
    `also called as json`, that is incorrect. JSON is a *string representation* of data that is a "subset of the object literal notation of JavaScript". If it's not a string, it's *not* JSON. – gen_Eric Nov 19 '13 at 21:26
  • 1
    You can also do `var obj = {};` instead of `var obj = new Object();` :-) – gen_Eric Nov 19 '13 at 21:30
  • 1
    :) yes various ways. Javascript objects are just containers to hold key-value pairs – BSS Nov 19 '13 at 21:31
-4

Those are JSON Objects that can be accessed like so:

var type = defaultVar.type;
tylerargo
  • 1,000
  • 10
  • 13
  • There's no such thing as a "JSON object". He as an object literal. JSON is a *string representation* of data; if it's not a string, it's not JSON. The link says "JSON is a subset of the object literal notation of JavaScript". – gen_Eric Nov 19 '13 at 21:20
  • I have been served. That's what I get for trying to pick the low hanging fruit :P Thank you for the clarification. – tylerargo Nov 19 '13 at 21:23
-5

This is json syntax: http://en.wikipedia.org/wiki/JSON

this is used to create an associative array.

AJames
  • 64
  • 3
  • This is by no means to create an _associative array_, and it definitely isn't JSON – dbf Nov 19 '13 at 21:18
  • 2
    Not quite. JavaScript doesn't have associative arrays. Also, JSON is only when it's in a *string context*. If it's not a string, it's not JSON. – gen_Eric Nov 19 '13 at 21:18