The object class in JavaScript

Source: Internet
Author: User


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

    1. An object is represented by a pair of {} curly braces

    2. An object can contain several key-value pairs of data, a key must be of type string, and value can be of any type

    3. In some of the less restrictive notation, you can omit double or single quotes on either side of key, but this is never recommended.

    4. A key-value pair is used internally: a colon separates key and value, and a comma separates the different key-value pairs

    5. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.