JavaScript Object and JSON
Full information about following notes is on www3school.
JavaScript Object
A JavaScript is wrapped by {}
or created by new
. Components inside are like key:value pairs.
i.e. JavaScript objects are containers for named values.
An example of JavaScript Object:
var car = { type: "Fiat", model: 500, color: "silver", buycar: function(money){ return money * 115%; } };
Each JavaScript has properties and methods.
Properties
In the example above, type
, model
and color
are properties. You can access properties by using object_name.property_name
to get value of certain property.
car.type //'Fiat' //Or car[type]//'Fiat'
A JavaScript object has two build-in properties: length
and prototype
Methods
You can create methods in a JavaScript object. In code above, butcar
is a method. A method can be defined by a function.
A method may or may not have parameters A method may or may not have return values
car.buycar(100) //115
A JavaScript object has a list of build-in method. You can view them here.
Tips
-
To change JSON to JavaScript object, you can use
JSON.parse(json_object)
or jQuery method:$.parseJSON(json_object)
-
String
,Number
andBoolean
should not be declared as objects, which means code likevar x = new String()
is not recommended.
JSON
JSON means JavaScript Object Notation. It has following rules:
-
Data is in name/value pairs
-
Data is separated by commas
,
-
Curly braces
{}
hold objects -
Square brackets
[]
hold arrays
To operate JSON, you can change a JSON to a JavaScript object, then use properties and methods of that JavaScript object.
Example of JSON:
var text = '{ "employees" : [' + '{ "firstName":"John" , "lastName":"Doe" },' + '{ "firstName":"Anna" , "lastName":"Smith" },' + '{ "firstName":"Peter" , "lastName":"Jones" } ]}'; /* * 1. This JSON has a large JavaScript object, which has a property: 'employees' * 2. The value of the 'employees' property is an JavaScript objects array. * 3. The array has four JavaScript objects, each object has two properties: 'firstName' and 'lastName', with property values. */
Tips
- To change a JavaScript objec to JSON, using
JSON.stringify(js_object)