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 name attribute to the object ='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 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 name attribute to the object ='JS' /**Access object property value */ //1. How to use dot numbers var x=;//12 var authorOfName=;//JS var name=;//JS //2. How to use array var x2=o2['x'];//12 var authorOfName2=o3['author']['name'];//JS var name2=o4['name'];//JS
It 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 attributes, x, y, name var o2={x:12,y:'12',name:'JS'}; for(p in o2) { var property=p; var value=o2[p]; (property); (value); }
The print result is:
x 12 y 12 name JS
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. How to 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 attributes, x, y, name var o2={x:12,y:'12',name:'JS'}; var b=('name')//true var b2=('age')//false
3. 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 attributes, x, y, name var o2={x:12,y:'12',name:'JS'}; //Delete the name attribute delete ; var b=('name')//false //Add a new name attribute o2['name']='javascript'; // Since the name attribute already exists, here is to reassign the name to reassign the name ='js'; var b3=('name');//true
Above, 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 class libraries 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.
Convert an object into a string to use (); convert a string into an object to use (),
//Define an object var o={name:'JavaScript',age:24}; //This method is an error when converting to an object, and the following method must be used //var str="{name:'JavaScript',age:24}"; //Correctly define object string var str='{"name":"JavaScript","age":24}'; //Convert object into string var str2=(o); ('str2:'+str2+',type:'+(typeof str2));//str2:{"name":"JavaScript","age":24}, Type: string //Convert strings into objects var o2=(str); ('o2:'+o2+',type:'+(typeof o2));//o2:[object Object],type:object
The 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 you can give you a reference and I hope you can support me more.