Home >Web Front-end >JS Tutorial >Detailed explanation of javascript dynamically adding, modifying, and deleting object attributes and methods_javascript skills
Now we will introduce how to add, modify or delete properties and methods for an object. In other languages, once an object is generated, it cannot be changed. To add modified members to an object, it must be modified in the corresponding class and re-instantiated, and the program must be recompiled. This is not the case in JavaScript, which provides a flexible mechanism to modify the behavior of objects and can dynamically add, modify, and delete properties and methods. For example, first use the class Object to create an empty object user:
var user=new Object();
1. Add attributes
At this time, the user object does not have any attributes and methods, which is obviously of no use. But you can add attributes and methods to it dynamically, for example:
user.name="jack";
user.age=21;
user.sex="male";
through the above statement , the user object has three attributes: name, age and sex. Output these three statements below:
alert(user.name);
alert(user.age);
alert(user.sex);
It can be seen from the code running effect that the three attributes have It completely belongs to the user object.
2. Adding methods
The process of adding methods is similar to attributes:
user.alert=function(){
alert(“my name is:” this.name);
}
This adds a method "alert" to the user object. By executing it, a dialog box will pop up to display your name introduction:
user.alert();
3. Modify attributes
The process of modifying an attribute is to replace the old attribute with a new attribute, for example:
user.name="tom";
user.alert=function(){
alert("hello," this.name);
}
This modifies the value of the name attribute of the user object and the alert method. It changes from displaying "my name is" to displaying "hello".
4. Deleting an attribute
The process of deleting an attribute is also very simple, which is to set it to undefined:
user.name=undefined;
user.alert=undefined;
This way the name is deleted Properties and alert methods. Later in the code, these properties become unavailable.
When adding, modifying or deleting attributes, you can also use square brackets ([]) syntax like reference attributes:
user[“name”]=”tom”;
Using this method can also An additional feature is that you can use non-identifier strings as attribute names. For example,
does not allow identifiers to start with numbers or spaces, but can be used in square brackets ([]) syntax:
user[“my name”]=”tom”;
It should be noted that when using this non-identifier as a name attribute, you still need to use square bracket syntax to quote:
alert(user[“my name"]);
but cannot be written as:
alert(user.my name);
Using this property of objects, it is even easy to implement a simple hash table. In this book We will see its application later. It can be seen that every object in JavaScript is dynamically variable, which brings great flexibility to programming and is also very different from other languages. Readers can appreciate this property.