Simple record of the use of objects in javascript
1. Create an object
//Create an empty object var o={}; //Create an object with two attributes, x and y var o2={x:12,y:'12',name:'JS'}; //The value of the author attribute in this object is still an object var o3={x:12,author:{name:'JS',age:23,address:'china'}}; //Create an empty object the same as {} var o4=new Object(); //Add the name attribute to the object o4.name='JS'The above uses two ways to create objects, one is the literal method, and the other is to create objects using new. The Object after new is called a constructor.
2. Object access
From the above we can see that we have added an attribute name to the opposing o4, using the dot number, that is, the object name and the attribute name, which is one of the access methods. There are two ways to access attribute values in an object. The first is to use dot numbers (.), and the second is to use arrays (object name [attribute name]).
//Create an empty object var o={}; //Create an object with two attributes, x, y var o2={x:12,y:'12',name:'JS'}; //The value of the author attribute in this object is still an object var o3={x:12,author:{name:'JS',age:23,address:'china'}}; //Create an empty object the same as {} var o4=new Object(); //Add name attribute o4.name='JS' /**Accessing the object's attribute value*/ //1. Use point number var x=o2.x;//12 var authorOfName=o3.author.name;//JS var name=o4.name;//JS //2. Use array method var x2=o2['x'];//12 var authorOfName2=o3['author']['name'];//JS var name2=o4['name'];//JSIt is easier to understand how to use dot numbers to access attribute values in an object, but it is not easy to understand how to use arrays. In JavaScript, all objects are associative arrays. The so-called associative data is the way to access an array, but this method is not the index used but the string index, which is called associative arrays.
The above accesses the object attribute value are all about knowing the object attribute name. What if you don’t know the object attribute value? You can use for/in to loop through the values in the object.
//Create an object with two properties, x, y, name var o2={x:12,y:'12',name:'JS'}; for(p in o2) { var property=p; var value=o2[p]; console.log(property); console.log(value); }The print result is:
x12y12nameJS
It can be seen that there are three attributes in total, and their values are printed.
If the object is more complicated, you can add some judgments to determine whether there is a property. Then how do you determine whether an object contains a certain property? Since the objects all inherit Object, there is a hasOwnProperty() method in the Object to determine whether there is a property in the object. The return value is a boolean. Note that this method will only judge whether the object's own attributes exist, and will not judge the attributes inherited by the object.
//Create an object with two properties, x, y, name var o2={x:12,y:'12',name:'JS'}; var b=o2.hasOwnProperty('name')//true var b2=o2.hasOwnProperty('age')//false3. Add and delete attributes
At the beginning, we added a name attribute to object o4. The new method is actually the same as assigning attribute values. You can also add attributes to the object using the associative array.
//Create an object with two properties, x, y, name var o2={x:12,y:'12',name:'JS'}; //Delete name attribute delete o2.name; var b=o2.hasOwnProperty('name')//false //Add name attribute o2['name']='javascript'; //Because the name attribute already exists, here is to reassign the name o2.name='js'; var b3=o2.hasOwnProperty('name'); //TrueAbove, the name attribute of object o2 was deleted, and then the name attribute was added using the associative array, and then the name attribute was reassigned using the dot number.
4. Conversion between objects and strings
ECMAScript5 has built-in mutual conversion between objects and strings. Now most mainstream browsers support ECMAScript5. If it does not support it, you can download the json2.js class library from the Internet. This library can be used after introducing it into a file.
The conversion between an object and a string is called object serialization, which means converting the state of the object into a string or converting a string into an object. These conversions use JSON as the data exchange format. The full name of JSON is JavaScript Object Notation.
Use JSON.stringify() to convert the object into a string; use JSON.parse() to convert the string into an object.
//Define an object var o={name:'JavaScript',age:24}; //This method is an error when converting to an object. You must use the following method //var str="{name:'JavaScript',age:24}"; //Correctly define the object string var str='{"name":"JavaScript","age":24}'; //Convert the object into a string var str2=JSON.stringify(o); console.log('str2:'+str2+',type: '+(typeof str2)); //str2:{"name":"JavaScript","age":24},type: string //Convert the string to an object var o2=JSON.parse(str); console.log('o2:'+o2+', type: '+(typeof o2));//o2:[object Object], type: objectThe above implements mutual conversion between objects and strings.
The above brief discussion on the use of objects in js is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.