The object class is the most basic class in JavaScript and is used to create generic objects that typically store data with these objects.
1. Object objects
1.1. Creating Object Objects
There are two ways of creating object objects
first method of creation : New Object ()
var obj = new Object ();
The second method of creation : {}
var obj = {};var Obj2 = {"Name": "Tom", "Age": 24};
1.2. Adding attributes
Obj.firstname = "Hello"; obj.lastname = "World";
1.3. Get Property values
var FirstName = Obj.firstname;var LastName = obj["LastName"];
1.4. Traverse Properties
for (var key in obj) {alert (key + ":" + Obj[key]);}
Complete example
<! doctype html public "-//w3c//dtd html 4.01 transitional//en" "HTTP// Www.w3.org/TR/html4/loose.dtd ">
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/86/9C/wKiom1fFRsSQAovQAANn6uFcwwE617.gif "title=" Javascript_for_obj.gif "alt=" Wkiom1ffrssqaovqaann6ufcwwe617.gif "/>
2. JSON
2.1. Initial knowledge of JSON
var obj = {"Name": "Tom", "Age": 24};
JSON (Javascript object Notation), that is, how JS represents an object
The data in JSON format can be directly recognized as JS object by JS, because JS is the object in memory.
2.2. JSON syntax format
An object is represented by a pair of {} curly braces
An object can contain several key-value pairs of data, a key must be of type string, and value can be of any type
In some of the less restrictive notation, you can omit double or single quotes on either side of key, but this is never recommended.
A key-value pair is used internally: a colon separates key and value, and a comma separates the different key-value pairs
In particular, an array object is represented by a pair of [] brackets, and the array element can be any type of value
The object class in JavaScript