This article describes how to check, add, delete, and access operation instances of JavaScript objects, this article provides code examples to explain how to add attributes to an object, check the existence of attributes, and delete and access attributes. For more information, see
Check attributes
var mouse = { "name": "betta", "age": 3, "varieties": "milaoshu"} mouse.hasOwnProperty("name"); // truemouse.hasOwnProperty("sex"); //false
Add attribute
Define an object dog, then assign various features, then assign color features, and finally traverse all attributes and values
Var dog = {name: "Mango", type: "King of the Meeting", eat: function () {alert ("eat") ;}} Object. prototype. color = "white"; var name; for (name in dog) {document. write (name + "" + dog [name] +"
")}
The effect is as follows:
Name mango type club king eat function () {alert ("eat");} color white
Delete attributes
Var cat = {"name": "tom", "sex": "man", "color": "yellow"} delete cat. name; cat. sex = undefined; cat. color = null; alert ("whether the name attribute exists:" + cat. hasOwnProperty ("name"); // falsealert ("existence of sex property:" + cat. hasOwnProperty ("sex"); // truealert ("Whether the color attribute exists:" + cat. hasOwnProperty ("color"); // true
Access attributes
Var cat = {"name": "tom", "sex": "man", "color": "yellow"} var name1 = cat. name; // use the dot operator to access the object attribute var name2 = cat ["name"]; // use the brackets operator to access the object attribute
There are two ways to create an object
Var obj = new Object (); obj. name = "MangGuo"; obj. age = 25; var obj = {name: "MangGuo", // name is the property name, and "MangGuo" is the value age: 25}