Javascript dynamically adds modifies and deletes properties and methods of objects

  • 2020-03-30 01:34:22
  • OfStack

Now we show you how to add, modify, or remove properties and methods for an object. In other languages, once an object is generated, it cannot be changed, adding a modified member to an object must be modified in the corresponding class, reinstantiated, and the program must be recompiled. This is not the case in JavaScript, which provides a flexible mechanism to modify the behavior of an object by dynamically adding, modifying, and deleting 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 point the user object has no properties or methods, and obviously no purpose. But you can add properties and methods to it dynamically, for example:
The user. The name = "jack";
The user. The age = 21;
User. Sex = "male";
With the statement above, the user object has three properties: name, age, and sex. The following three statements are output:
Alert (user) name);
Alert (user. Age);
Alert (user. Sex);
As the code runs, the three properties are already fully owned by the user object.

2. Add methods
The process of adding methods and properties is similar:
User. Alert = function () {
Alert (" my name is: "+ enclosing name);
}
This adds a method "alert" to the user object. By executing it, you can pop up a dialog box showing your name:
The user. The alert ();

3. Modify the properties
The process of modifying a property is to replace an old property with a new one, for example:
The user. The name = "Tom";
User. Alert = function () {
Alert (" hello, "+ enclosing name);
}
This changes the value of the name attribute of the user object and the alert method, which changes from "my name is" to "hello".

4. Delete the properties
Deleting a property is also as simple as setting it undefined:
The user. The name = undefined;
User. Alert = undefined;
This removes the name attribute and alert method. In later code, these properties become unavailable.
When you add, modify, or delete attributes, you can use the same square bracket ([]) syntax as when you reference attributes:
The user (" name ") = "Tom";
An additional feature of this approach is that you can use non-identifier strings as attribute names, for example
Identifiers are not allowed to start with Numbers or Spaces, but in the square bracket ([]) syntax:
[" my user name "] = "Tom";
Note that when you use this non-identifier as a name attribute, you still use the square bracket syntax to refer to it:
Alert (user (" my name "));
Instead of writing:
Alert (user. My name);
Taking advantage of this property of objects, it's even easy to implement a simple hash table, which you'll see applied later in this book. As you can see, every object in JavaScript is dynamically mutable, which brings a lot of flexibility to programming and makes it very different from other languages, as readers can appreciate.


Related articles: