Example of adding and removing object properties in JavaScript _javascript tips

Source: Internet
Author: User
Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>insert title here</title>
<script type= "Text/javascript" >
/*
* use. To add properties to an object
Delete an attribute with the keyword delete

Add attributes with []
The difference between the and.
r.name==r["Name"];
R.name can only add string properties
[] Add properties, powerful, can add properties dynamically
*/
var r=new Object ();
Alert (typeof (R));
Add properties
R.name= "Jobs";
alert (r.name);
R.getname=function () {
return "Jobs";
}
Alert (R.getname ());
Delete attribute
Delete R.name;
Alert (R.name)//The result of this is undefined.
//--------------------------------------------
To add a property to an object by using the brackets
r["Name"]= "Tom";
Alert (r["name"]);

R.name= "Toms";
Alert (r["names"]);
r.name==r["Name"]//these two ways are equivalent.
var arg= "abc";
R[arg]= "Heima";//powerful, can dynamically add properties, you can make a function variable returned a string
R.arg= "Itcast";//arg default to String
</script>
<body>

</body>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.