How to dynamically add, modify, and delete objects using javascript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the attributes and methods for dynamically adding, modifying, and deleting objects in javascript. If you need them, refer to them, I hope it will help you to introduce how to add, modify, or delete attributes and methods for an object. In other languages, once an object is generated, it cannot be changed. To add a modified member to an object, you must modify it in the corresponding class and instantiate it again, and the program must be re-compiled. This is not the case in JavaScript. It provides a flexible mechanism to modify the behavior of objects and dynamically add, modify, and delete attributes and methods. For example, first create an empty Object user using a class Object:
Var user = new Object ();

1. Add attributes
At this time, the user object does not have any attributes and methods and is obviously useless. However, you can dynamically add attributes and methods for it, for example:
User. name = "jack ";
User. age = 21;
User. sex = "male ";
With the preceding statement, the user object has three attributes: name, age, and sex. The following three statements are output:
Alert (user. name );
Alert (user. age );
Alert (user. sex );
The code running result shows that the three attributes are completely user objects.

2. Add Method
The process of adding a method is similar to that of adding a property:
User. alert = function (){
Alert ("my name is:" + this. name );
}
This adds a method "alert" for the user object. By executing this method, a dialog box is displayed, showing the description of your name:
User. alert ();

3. modify attributes
The process of modifying an attribute is to replace the old attribute with the new attribute, for example:
User. name = "tom ";
User. alert = function (){
Alert ("hello," + this. name );
}
In this way, the value of the name attribute of the user object and the alert method are modified. It is changed from "my name is" to "hello ".

4. Delete attributes
The process of deleting an attribute is also very simple, that is, setting it to undefined:
User. name = undefined;
User. alert = undefined;
In this way, the name attribute and alert method are deleted. In subsequent Code, these attributes become unavailable.
When adding, modifying, or deleting an attribute, it is the same as the referenced attribute. You can also use the square brackets ([]) Syntax:
User ["name"] = "tom ";
Additionally, you can use a non-identifier string as the property name, for example
The identifier cannot start with a number or contain spaces, but can be used in the square brackets ([]) Syntax:
User ["my name"] = "tom ";
Note that when using this non-identifier as the name attribute, you still need to reference it using square brackets Syntax:
Alert (user ["my name"]);
But cannot be written:
Alert (user. my name );
Using this property of objects, you can even easily implement a simple hash table, which will be seen later in this book. It can be seen that every object in JavaScript is dynamically changeable, which brings great flexibility to programming and makes a big difference with other languages. Readers can understand this nature.

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.