In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis on the operation of JavaScript attributes. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
1. Setting and acquisition of attributes 1. There are two main ways to set and obtain attributes: the basic operation of attributes in JavaScript _ window.onload=function () {/ / the setting of attributes or the acquisition of var obj= {}; / / through "." Set the property obj.name= "tom"; / / set the property obj ["age"] = 20 through "[]"; / / pass "." Console.log ("name:" + obj.name); / / get the attribute console.log ("age:" + obj ["age"]) by "[]";}
Running result:
2. The difference between the two ways the basic operation of attributes in JavaScript _ window.onload=function () {/ / property setting or getting var obj= {}; / / through "." Set the property obj.name= "tom"; / / set the property obj ["age"] = 20 through "[]"; / / pass "." Console.log ("name:" + obj.name); / / get the attribute console.log ("age:" + obj ["age"]) by "[]"; / / distinguish / / "." The number can only take its own attribute. "[]" can be a variable or its own attribute var obj1= {name: "jack", age:18}; / / define a variable var a = "name"; console.log (obj2 [a]); / / equivalent to console.log (obj2 ["name"]) / / console.log (obj2.a) is incorrectly written. You can only take your own property console.log (obj2.name);}
Running result:
II. Deletion of attributes
Look at the following example:
The basic operation of the attribute in JavaScript _ window.onload=function () {/ / the setting or acquisition of / * var obj= {}; / / passes the "." Set the property obj.name= "tom"; / / set the property obj ["age"] = 20 through "[]"; / / pass "." Console.log ("name:" + obj.name); / / get the attribute console.log ("age:" + obj ["age"]) by "[]"; / / distinguish / / "." The number can only take its own attribute. "[]" can be a variable or its own attribute var obj1= {name: "jack", age:18}; / / define a variable var a = "name"; console.log (obj2 [a]); / / equivalent to console.log (obj2 ["name"]) / / console.log (obj2.a) is incorrectly written. You can only take your own attribute console.log (obj2.name); * / / delete var obj2= {name: "jack", age:18, sex: "male", email: "747934521@qq.com"}; / / pass. Delete age attribute delete obj2.age console.log (obj2); / / delete sex attribute delete obj2 ["sex"]; console.log (obj2) through []; / / you can also delete var temp= "email"; delete obj2 [temp]; console.log (obj2);}
Running result:
Third, determine whether the attribute exists. 1. Use the in operator. If there is a return true for a property, but there is no return false for a property, take a look at the following example: the basic operation of the property in JavaScript, _ window.onload=function () {/ / property setting or acquisition / * var obj= {}; / / through "." Set the property obj.name= "tom"; / / set the property obj ["age"] = 20 through "[]"; / / pass "." Console.log ("name:" + obj.name); / / get the attribute console.log ("age:" + obj ["age"]) by "[]"; / / distinguish / / "." The number can only take its own attribute. "[]" can be a variable or its own attribute var obj1= {name: "jack", age:18}; / / define a variable var a = "name"; console.log (obj2 [a]); / / equivalent to console.log (obj2 ["name"]) / / console.log (obj2.a) is incorrectly written. You can only take your own attribute console.log (obj2.name); * / / delete / * var obj2= {name: "jack", age:18, sex: "male", email: "747934521@qq.com"}; / / pass. Delete age attribute delete obj2.age console.log (obj2); / / delete sex attribute delete obj2 ["sex"]; console.log (obj2); / / you can also delete var temp= "email"; delete obj2 [temp]; console.log (obj2) via [] * / / Detection of attributes / / in operator determines whether var obj3= {name: "jack", age:18}; console.log ("name" in obj3); console.log ("sex" in obj3);}
Running result:
2. Use the hasOwnProperty () method of the object
Similarly, if there is a return true for a property and a return false for a non-existent property, see the following example:
The basic operation of the attribute in JavaScript _ window.onload=function () {/ / the setting or acquisition of / * var obj= {}; / / passes the "." Set the property obj.name= "tom"; / / set the property obj ["age"] = 20 through "[]"; / / pass "." Console.log ("name:" + obj.name); / / get the attribute console.log ("age:" + obj ["age"]) by "[]"; / / distinguish / / "." The number can only take its own attribute. "[]" can be a variable or its own attribute var obj1= {name: "jack", age:18}; / / define a variable var a = "name"; console.log (obj2 [a]); / / equivalent to console.log (obj2 ["name"]) / / console.log (obj2.a) is incorrectly written. You can only take your own attribute console.log (obj2.name); * / / delete / * var obj2= {name: "jack", age:18, sex: "male", email: "747934521@qq.com"}; / / pass. Delete age attribute delete obj2.age console.log (obj2); / / delete sex attribute delete obj2 ["sex"]; console.log (obj2); / / you can also delete var temp= "email"; delete obj2 [temp]; console.log (obj2) via [] * / / Detection of attributes / / in operator determines whether attributes in the object exist / * var obj3= {name: "jack", age:18}; console.log ("name" in obj3); console.log ("sex" in obj3) * / / use the object's hasOwnProperty () method var obj4= {name: "jack", age:18}; console.log (obj4.hasOwnProperty ("name")); console.log (obj4.hasOwnProperty ("sex"));}
Running result:
Note: you can also use variables to determine whether an attribute exists, for example:
3. Use undefined to judge
If the value of the property is not equal to undefined, it indicates that the property exists and returns true. If the value of the property is equal to undefined, it indicates that the property does not exist and returns false. Look at the following example:
The basic operation of the attribute in JavaScript _ window.onload=function () {/ / the setting or acquisition of / * var obj= {}; / / passes the "." Set the property obj.name= "tom"; / / set the property obj ["age"] = 20 through "[]"; / / pass "." Console.log ("name:" + obj.name); / / get the attribute console.log ("age:" + obj ["age"]) by "[]"; / / distinguish / / "." The number can only take its own attribute. "[]" can be a variable or its own attribute var obj1= {name: "jack", age:18}; / / define a variable var a = "name"; console.log (obj2 [a]); / / equivalent to console.log (obj2 ["name"]) / / console.log (obj2.a) is incorrectly written. You can only take your own attribute console.log (obj2.name); * / / delete / * var obj2= {name: "jack", age:18, sex: "male", email: "747934521@qq.com"}; / / pass. Delete age attribute delete obj2.age console.log (obj2); / / delete sex attribute delete obj2 ["sex"]; console.log (obj2); / / you can also delete var temp= "email"; delete obj2 [temp]; console.log (obj2) via [] * / / Detection of attributes / / in operator determines whether attributes in the object exist / * var obj3= {name: "jack", age:18}; console.log ("name" in obj3); console.log ("sex" in obj3) * / / use the object's hasOwnProperty () method / * var obj4= {name: "jack", age:18}; console.log (obj4.hasOwnProperty ("name")); console.log (obj4.hasOwnProperty ("sex")); / / use variables to determine var temp= "name" Console.log (obj4.hasOwnProperty (temp)); var temp1= "email"; console.log (obj4.hasOwnProperty (temp1)); * / / determine whether the value of the attribute is not equal to undefined var obj5= {name: "jack", age:18}; console.log (obj5.nameplate attribute undefined) Console.log (obj5.sexuality undefined);}
Running result:
Note: if the value of the property happens to be undefined, then this judgment cannot be used at this time. Take a look at the following example:
4. Traversal of the attribute 1. Use for...in to traverse the basic operation of the attribute in the attribute JavaScript _ window.onload=function () {/ / the setting or acquisition of / * var obj= {}; / / through "." Set the property obj.name= "tom"; / / set the property obj ["age"] = 20 through "[]"; / / pass "." Console.log ("name:" + obj.name); / / get the attribute console.log ("age:" + obj ["age"]) by "[]"; / / distinguish / / "." The number can only take its own attribute. "[]" can be a variable or its own attribute var obj1= {name: "jack", age:18}; / / define a variable var a = "name"; console.log (obj2 [a]); / / equivalent to console.log (obj2 ["name"]) / / console.log (obj2.a) is incorrectly written. You can only take your own attribute console.log (obj2.name); * / / delete / * var obj2= {name: "jack", age:18, sex: "male", email: "747934521@qq.com"}; / / pass. Delete age attribute delete obj2.age console.log (obj2); / / delete sex attribute delete obj2 ["sex"]; console.log (obj2); / / you can also delete var temp= "email"; delete obj2 [temp]; console.log (obj2) via [] * / / Detection of attributes / / in operator determines whether attributes in the object exist / * var obj3= {name: "jack", age:18}; console.log ("name" in obj3); console.log ("sex" in obj3) * / / use the object's hasOwnProperty () method / * var obj4= {name: "jack", age:18}; console.log (obj4.hasOwnProperty ("name")); console.log (obj4.hasOwnProperty ("sex")); / / use variables to determine var temp= "name" Console.log (obj4.hasOwnProperty (temp)); var temp1= "email"; console.log (obj4.hasOwnProperty (temp1)); * / / determine whether the value of the attribute is not equal to undefined / * var obj5= {name: "jack", age:18}; console.log (obj5.nameattribute undefined) Console.log (obj5. Sexuality unknown undefined); / / var obj6= {name:undefined, age:18}; console.log (obj6.nameplate orientation undefined) * / / traversal of attributes / / for...in traverses the properties of the object var obj7= {a: "1", b: "2", c: "3"} For (var v in obj7) {/ / output the attribute console.log (v) of obj7; / / output the corresponding value console.log (obj7 [v]);}}
Running result:
For..in can also traverse the array:
5. Serialization
1. JSON.stringify () serializes the object into a string in JSON format, which is used when the front end passes the data to the backend, for example:
2. JSON.parse () converts a string in JSON format into an object, which is used when passing data to the front end in the background, for example:
This is the end of this article on "sample analysis of JavaScript attribute operations". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 287
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.