Example of adding and deleting object attributes in JavaScript

Source: Internet
Author: User

Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> Insert title here </title>
<Script type = "text/javascript">
/*
* Use. To add attributes to an object
Delete attributes with the keyword "delete"

Use [] to add attributes
Different from.
R. name = r ["name"];
R. name can only add string attributes
[] Adding a property. It is powerful and can be dynamically added.
*/
Var r = new Object ();
Alert (typeof (r ));
// Add attributes
R. name = "jobs ";
Alert (r. name );
R. getname = function (){
Return "jobs ";
}
Alert (r. getname ());
// Delete attributes
Delete r. name;
Alert (r. name); // The result is undefined.
//--------------------------------------------
// Use brackets to add attributes for objects
R ["name"] = "tom ";
Alert (r ["name"]);

R. name = "toms ";
Alert (r ["names"]);
// R. name = r ["name"] // the two statements are equivalent.
Var arg = "abc ";
R [arg] = "heima"; // powerful function. You can dynamically add attributes to make a string returned by a function variable
R. arg = "itcast"; // arg is a string by default.
</Script>
</Head>
<Body>

</Body>
</Html>

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.