This article describes the basic usage of js objects. Share it for your reference. The specific analysis is as follows:
JS objects are essentially the same as arrays, and they store a set of data. But the creation method is different, and the object needs to be added.
Here we briefly describe the creation, reading/traversal of objects, as follows:
Copy the code as follows: <html>
<head>
</head>
<body>
<script type="text/javascript">
var stu = {name:'Wang Meiren',age:25,num:'10935'};//Create an object
document.write(stu['name'] + '<br />');//Object reading method 1
document.write(stu.age + '<br />');//Object reading method two
//Transf the object. for in structure
for(per in stu){// means to traverse the object, and every time the object's attribute value is assigned to per
document.write(stu[per] + '<br />');//When traversing the object, you cannot read it in the form of "obj. attribute", you can only use "obj[attribute]".
}
//Delete a unit in the object
delete stu.num;
//Methods in the object
var stu = {name:'Minister Wang',age:25,num:'10935',talk:function(){alert('Hello everyone, my name is Meiren Wang')}};//Create an object
stu.talk();//Read the method in the object
</script>
</body>
</html>
In addition, if a method in the object needs to call a certain attribute in the object, use this keyword, such as this. attribute name
I hope this article will be helpful to everyone's JavaScript programming.