SoFunction
Updated on 2025-02-28

Detailed explanation of JavaScript objects to add object properties

1. What is an object?

The English name is object, and it is the object translated into Chinese. From an English perspective, object is an object entity, even if it cannot be seen or touched. The object in Chinese refers to the girlfriend. In computers, understanding objects from an English perspective means that complex data sets placed in memory, also known as encapsulation of data and methods, is a programming logic concept.

Functions are the encapsulation of data and code. If the function and data outside the function are encapsulated, it is object, that is, object.

2. Create an object

Encapsulating some functions and objects is an object. The so-called encapsulation at the syntax level means wrapping functions and variables in English braces {}. Use the form of:key:value, which can be the value of the object or the address of the object.

The value of the key may not comply with the naming specification of the identifier, but must be enclosed in quotes, such as '12qw'=1. Each key-value pair is separated by an English comma.

//Create an obj objectvar obj1 = {
    str1: 'woaini',
    "10p": 10
};
function f() {
    (1)
};
var obj1 = {
    str1: 'woaini',
    "10p": 10,
    fun: f,
    fun1: function() {
        (2)
    }
};
();
obj1.fun1();

If the value of a key is a function, then the key is called the method of this object. If the value of a key is a basic data type, the key is called the property of the object.

Three, nesting of objects

That is, the object's properties can still be an object. Operator. represents the meaning of getattr, that is, accessing object properties.

var obj1 = {
    str1: 'woaini',
    "10p": 10,
    fun1: function() {
        (2)
    },
    obj_inn: obj2 = {
        num: 1
    }
};
(obj1.obj_inn.num);

4. Object properties and modification

1. Use dot operators

var obj1 = {
    str1: 'woaini',
};
(obj1.str1);

2. Use the [] symbol

var obj1 = {
    str1: 'woaini',
};
(obj1['str1'])

Remember that the keys need to be quoted.

3. Modify properties

var obj1 = {
    str1: 'woaini',
};
obj1.str1 = 666
(obj1['str1'])

5. Add attributes to the object

var obj1 = {
};
 = 'xiaoming';
obj1['age'] = 10;
(, );

6. View and delete the properties of the object

1. Use the (obj) method to view all the properties of the object

var obj1 = {
    str1: 'woaini',
};
obj1.str1 = 666
 = 18
((obj1))
// [ 'str1', 'age' ]

2. Use delete() method to delete object properties

var obj1 = {
    str1: 'woaini',
};
obj1.str1 = 666
 = 18
(delete )
// true

Deleting properties that do not exist in an object will not only not report an error, but will also return true. False is returned when trying to delete an attribute that cannot be deleted. Delete object properties: In fact, it is unbinding with the related objects.

3. Use the enhanced version for loop through object elements

var obj1 = {
    str1: 'woaini',
};
obj1.str1 = 666
 = 18
for (var item in obj1) {
    (obj1[item])
}

7. Object object method

Here we explain some knowledge about Object objects. It is similar to a base class and is the boss of all objects.

1. The method of the Object object itself

Object itself is an object, and you can add properties and methods to it. The method that uses key-value pairs to add to an object is called the Object itself method. Only use () to execute.

 = function() {
    (1)
}
()

2. Object object's own instance method

Methods added using the form () are called instance methods of objects. Can be used by any object.

function f() {
    (1)
};
 = f;
var obj = {}
()

8. Points to pay attention to functions and objects

1. Call other functions in the function body

You can call another function in the function body of another function, that is, function name +().

2. The number of function lines does not exceed 50 lines

The function body of each function does not exceed 50 rows. If it exceeds it, it is best to split it and use functions to build blocks to implement the function.

Summarize

This is the article about the detailed explanation of JavaScript objects. This is the introduction to this article. For more related contents for adding js object properties, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!