JS objects in use

Source: Internet
Author: User
Tags object serialization hasownproperty

Simple recording of the use of objects in JavaScript

First, create the object

 //  Create an empty object  var  o={};  //  Create an object with two attributes, X, y  var  o2={x:12,y: ' n ', Name: ' JS ' };  //  The value of the Author property in this object is also an object  var  o3={x:12,author:{name: ' JS ', age:23,address: ' China ' }};  //  Create an empty object and {}  var  o4=new   Object ();  //  add Name property to Object  o4.name= ' JS ' 

The above uses two ways to create objects, one is literal, the other is to create an object with new, and the object after new is called a constructor.

Ii. Access to Objects

From the above we can see that we have added a property name to the O4, using the Dot method, that is, the object name. The name of the property, which is one way to access it. There are two ways to access property values in an object, the first is to use a dot (.), and the second is to use an array (object name [property name]).

    //Create an empty object    varo={}; //create an object with two attributes, X, y    varO2={x:12,y: ' A ', Name: ' JS '}; //The value of the Author property in this object is also an object    varO3={x:12,author:{name: ' JS ', age:23,address: ' China '}}; //create an empty object as well as {}    varo4=NewObject (); //Add Name property to ObjectO4.name= ' JS '/** Accessing the object's property values*/    //1. How to use the dot number    varx=o2.x;// A    varAuthorofname=o3.author.name;//JS    varName=o4.name;//JS        //2. How to use arrays    varx2=o2[' x '];// A    varauthorofname2=o3[' author ' [' Name '];//JS    varname2=o4[' name '];//JS

Using the Dot method to access the property values in the object is better understood, but using arrays is not very well understood, in JavaScript, all objects are associative arrays, so-called associative data is the way that looks like an array of access, This is simply not the index used but the string index, which is called an associative array.

The above Access object property value is in the case of knowing the object property name, if you do not know the object's property value? You can use the For/in loop to iterate through the values in the object.

    // Create an object containing two attributes, X, y, name
var o2={x:12,y: ' n ', Name: ' JS '}; for inch O2) { var property=p; var value=o2[p]; Console.log (property); Console.log (value); }

Printing results are:

xynamejs

You can see that there are a total of three properties, and the values are printed.

If the object is more complex and you can add some judgments to determine if there is a property, then how do you know if an object contains a property, because the object inherits object, and in object it has the hasOwnProperty () method, which is used to determine whether an attribute exists in the object. The return value is a Boolean type (Boolean), note that this method only determines whether an object has its own property and does not determine the property that the object inherits.

    // Create an object containing two attributes, X, y, name    var o2={x:12,y: ' n ', Name: ' JS '};     var b=o2.hasownproperty (' name ')//true    var b2=o2.hasownproperty ( ' Age ')//false


Third, new, delete attributes

At the very beginning we added a Name property to the object O4, which is the same as assigning a value to a property, and you can add properties to an object using an associative array.

    //create an object with two attributes, X, y, name    varO2={x:12,y: ' A ', Name: ' JS '}; //Remove the Name property    DeleteO2.name; varB=o2.hasownproperty (' name ')//false    //New Name Propertyo2[' name ']= ' JavaScript '; //because the Name property already exists, this is the name re-assignmentO2.name= ' JS '; varB3=o2.hasownproperty (' name ');//true

Above, the Name property of the object O2 is removed, then the Name property is added using the associative array, and the Name property is re-assigned using the dot-number method.

Iv. conversions between objects and strings

In ECMAScript5, the conversion between objects and strings is built in, and most mainstream browsers now support ECMASCRIPT5, and if you do not support the ability to download the Json2.js class library from the Web, you can use it to bring the library into the file.

Conversions between objects and strings are called object serialization, which converts the state of an object into a string or converts a string into an object, all using JSON as the data Interchange format, and the full name of JSON is the JavaScript object Notation.

Convert the object to a string using json.stringify (); Convert the string to an object using Json.parse (),

    //defining an Object    varO={name: ' JavaScript ', age:24}; //in this way, the conversion to an object is an error and must be used in the following way    //var str= "{name: ' JavaScript ', age:24}";    //correct definition of object string    varStr= ' {' name ': ' JavaScript ', ' Age ': 24} '; //Convert an object to a string    varStr2=json.stringify (o); Console.log (' str2: ' +str2+ ', type: ' + (typeofSTR2));//str2:{"name": "JavaScript", "Age": 24}, type: String    //Convert A string to an object    varO2=json.parse (str); Console.log (' O2: ' +o2+ ', type: ' + (typeofO2));//O2:[object Object], type: Object

The above implements the conversion between the object and the string.

There are irregularities, welcome to point out, thank you!

JS objects in use

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.