Table of contents
Fourth, the properties and modification of the object
Five, add properties to the object
Six, view and delete the properties of the object
1. Use the Object.keys(obj) method to view all properties of the object
2. Use the delete() method to delete object properties
3. Use the enhanced for loop to traverse the object elements
2. Object object's own instance method
8. Points to note about functions and objects
1. Call other functions in the function body
2. The number of lines in the function body does not exceed 50 lines
One, what is an object?
The English name object, translated into Chinese is the object. From an English point of view, an object is an entity, even if it cannot be seen or touched. Object in Chinese refers to girlfriend. In a computer, understanding objects from an English perspective means: a complex data set stored in memory, also called the encapsulation of data and methods, is a programming logic concept.
A function is the encapsulation of data and code. If the function and the data outside the function are encapsulated, it is an object, that is, an object.
Second, create an object
Encapsulating some functions and objects is an object. The so-called encapsulation at the grammatical level is to wrap functions and variables with English braces {}. Use the form of: key: value, value can be the value of the object, or the address of the object.
The value of the key may not conform to the naming convention of the identifier, but it must be enclosed in quotation marks, such as '12qw'=1. Use commas to separate each key-value pair.
//创建一个obj对象
var obj1 = {
str1: 'woaini',
"10p": 10
};
function f() {
console.log(1)
};
var obj1 = {
str1: 'woaini',
"10p": 10,
fun: f,
fun1: function() {
console.log(2)
}
};
obj1.fun();
obj1.fun1();
A key is said to be a method of the object if its value is a function. If the value of a key is a primitive data type, the key is said to be a property of the object.
Third, the nesting of objects
i.e. a property of an object can still be an object. Operator. means getattr, that is, accessing object attributes.
var obj1 = {
str1: 'woaini',
"10p": 10,
fun1: function() {
console.log(2)
},
obj_inn: obj2 = {
num: 1
}
};
console.log(obj1.obj_inn.num);
Fourth, the properties and modification of the object
1. Use the dot. operator
var obj1 = {
str1: 'woaini',
};
console.log(obj1.str1);
2. Use the [] symbol
var obj1 = {
str1: 'woaini',
};
console.log(obj1['str1'])
Remember keys need to be quoted.
3. Modify attributes
var obj1 = {
str1: 'woaini',
};
obj1.str1 = 666
console.log(obj1['str1'])
Five, add properties to the object
var obj1 = {
};
obj1.name = 'xiaoming';
obj1['age'] = 10;
console.log(obj1.age, obj1.name);
Six, view and delete the properties of the object
1. Use the Object.keys(obj) method to view all properties of the object
var obj1 = {
str1: 'woaini',
};
obj1.str1 = 666
obj1.age = 18
console.log(Object.keys(obj1))
// [ 'str1', 'age' ]
2. Use the delete() method to delete object properties
var obj1 = {
str1: 'woaini',
};
obj1.str1 = 666
obj1.age = 18
console.log(delete obj1.age)
// true
Deleting a property that does not exist in an object not only does not report an error but also returns true. Returns false when attempting to delete a property that cannot be deleted. Delete object properties: actually unbind with related objects.
3. Use the enhanced for loop to traverse the object elements
var obj1 = {
str1: 'woaini',
};
obj1.str1 = 666
obj1.age = 18
for (var item in obj1) {
console.log(obj1[item])
}
Seven, Object object method
Here is some knowledge about the Object object, which is similar to the base class and is the boss of all objects.
1, Object object's own method
Object itself is an object, you can add properties and methods to him. The methods added to objects in the form of key-value pairs are called Object’s own methods. Can only be executed using Object.funcname().
Object.add = function() {
console.log(1)
}
Object.add()
2. Object object's own instance method
Methods added using the form Object.prototype.name() are called instance methods of the object. Can be used by any object.
function f() {
console.log(1)
};
Object.prototype.fun = f;
var obj = {}
obj.fun()
8. Points to note about functions and objects
1. Call other functions in the function body
You can call another function in the function body of a function, that is, the function name + ().
2. The number of lines in the function body does not exceed 50 lines
The function body of each function does not exceed 50 lines. If it exceeds, it is best to split it and use functions to build blocks to realize functions.