JavaScript Object Property check, add, delete, access operation instance, javascript instance
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] + "<br> ")}
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}