


Detailed explanation of how JavaScript adds, modifies or deletes attributes and method instances for objects
Introduces 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:
1 |
|
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:
1 2 3 |
|
Through the above statement, the user object has three attributes: name, age and sex. Output these three statements below:
1 2 3 |
|
It can be seen from the code running effect that the three attributes completely belong to the user object.
2. Adding methods
The process of adding methods is similar to attributes:
1 2 3 |
|
This adds a method "alert" to the user object. By executing it, a dialog box can pop up to display itself. Name introduction:
1 |
|
3. Modify attributes
The process of modifying an attribute is to replace the old attribute with a new attribute, for example:
1 2 3 4 |
|
This modifies the value of the name attribute of the user object and the alert method, which Changed 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:
1 2 |
|
This deletes the name attribute and alert method. Later in the code, these properties become unavailable.
When adding, modifying or deleting attributes, the same as referencing attributes, you can also use square brackets ([]) syntax:
1 |
|
There is an additional feature of using this method, that is You can use non-identifier strings as attribute names. For example,
identifiers do not allow numbers starting with numbers or spaces, but they can be used in square brackets ([]) syntax:
1 |
|
It should be noted that when using this non-identifier as a name attribute, you still need to use square bracket syntax to quote:
1 |
|
but cannot be written as:
1 |
|
Using this property of objects, it is even easy to implement a simple hash table, and you will see its application later in this book. 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.
The above is the detailed content of Detailed explanation of how JavaScript adds, modifies or deletes attributes and method instances for objects. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





1. First open DingTalk. 2. Open the group chat and click the three dots in the upper right corner. 3. Find my nickname in this group. 4. Click to enter to modify and save.

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Douyin Blue V certification is the official certification of a company or brand on the Douyin platform, which helps enhance brand image and credibility. With the adjustment of corporate development strategy or the update of brand image, the company may want to change the name of Douyin Blue V certification. So, can Douyin Blue V change its name? The answer is yes. This article will introduce in detail the steps to modify the name of the enterprise Douyin Blue V account. 1. Can Douyin Blue V change its name? You can change the name of Douyin Blue V account. According to Douyin’s official regulations, corporate Blue V certified accounts can apply to change their account names after meeting certain conditions. Generally speaking, enterprises need to provide relevant supporting materials, such as business licenses, organization code certificates, etc., to prove the legality and necessity of changing the name. 2. What are the steps to modify the name of corporate Douyin Blue V account?

Win10 Sleep Time Modification Tips Revealed As one of the currently widely used operating systems, Windows 10 has a sleep function to help users save power and protect the screen when not using the computer. However, sometimes the default sleep time does not meet the needs of users, so it is particularly important to know how to modify the Win10 sleep time. This article will reveal the tips for modifying the sleep time of Win10, allowing you to easily customize the system’s sleep settings. 1. Modify Win10 sleep time through “Settings” First, the simplest fix

When publishing products on the Xianyu platform, users can customize the geographical location information of the product according to the actual situation, so that potential buyers can more accurately grasp the specific location of the product. Once the product is successfully put on the shelves, there is no need to worry if the seller's location changes. The Xianyu platform provides a flexible and convenient modification function. So when we want to modify the address of a published product, how do we modify it? This tutorial guide will provide you with a detailed step-by-step guide. I hope it can help. Everyone! How to modify the release product address in Xianyu? 1. Open Xianyu, click on what I published, select the product, and click Edit. 2. Click the positioning icon and select the address you want to set.

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

In the job recruitment software called Boss Direct Jobs, users can personalize their greetings to better demonstrate their personality and job search intentions. However, sometimes the default greeting terms are rather mechanical and rigid. Many users want to make their own changes but don’t know where to set them. So this tutorial guide will introduce the detailed modification steps for players who want to know more. Come and follow this article to find out! How to change the answer when a boss directly hires someone to say hello: [Boss directly hires]-[My]-[Settings Icon]-[Greeting Language]. Specific steps: 1. First, we click on the boss direct recruitment software. After entering the homepage, we click [My] in the lower right corner; 2. Then when we come to the My page, we can see the relevant data.

Title: Go Language Programming Guide: Implementation of Modifying the Hosts File In the daily software development process, we often encounter situations where we need to modify the hosts file in the operating system. The Hosts file is a text file without extension used to map IP addresses and host names. It is usually used to set up domain name resolution locally. In this article, we will introduce how to use the Go language to implement the function of modifying the hosts file of the operating system, and give specific code examples. First, we need to create a G that can modify the hosts file
