JavaScript Object of Detail
- 2021-11-13 06:35:05
- OfStack
11. Enhanced for Loop (for in)
JavaScript object
1. Definition
An object is a reference data type, a container for holding complex data types, and a collection of multiple properties (data) and methods (functions)
It allows dynamic addition and deletion of attributes
2. Classification of objects
(1) Built-in Objects
Objects defined in the ES standard can be used in any implementation of ES
For example: math string number boolean object...
(2) Host object
Objects provided by the running environment of js are mainly provided by browsers at present
For example, BOM DOM
(3) Custom Objects
Objects created by developers themselves
3. Define objects
Object literal
Using object literals, you can specify the properties in an object directly when you create it
Syntax: {Attribute name: Attribute value, Attribute name: Attribute value...}
The attribute name of the literal amount of the object can be quoted or not, and it is recommended not to add it
If you want to use 1 special name, you must put quotation marks
With '{}' as the boundary, attributes are separated from attributes by ',' and attributes are separated from attribute values by ':'
var obj={};
Constructor to create an object
The function called using the new keyword is the constructor constructor
A constructor is a function dedicated to creating objects
When checking 1 object with typeof, object is returned
var obj = new Object();
console.log(typeof obj);
4. Access properties in an object
Point accessor.
o.name => 'terry'
o.age => 12
Access character
o['name'] => 'terry'
o['age'] => 12
If you read an attribute that is not in the object, you will not report an error but return undefined
5. Add attributes to objects
Object. Attribute = attribute value;
o.gender=' Female ';
6. Delete object properties
delete Object. Attributes;
delete o.gender;
7. Object root constructor
All objects directly or indirectly inherit from Object and can call methods in the Object prototype
var o=new Object();
var arr=new Array();
8. Detection of object attributes
in Operator
This operator allows you to check whether an object contains the specified property
If yes, true is returned, and if no, false is returned
Syntax:
"Attribute name" in object
'name' in Obj;
9. Reference passing and value passing
Basic data types use value passing: Basic data types save values directly to the stack area of memory
js The variables in are stored in stack memory
Values of basic data types are stored directly in stack memory
Value and value are independent, modify 1 A variable does not affect other variables
var a=123;
var b=a;
a++;
console.log('a='+a); //124
console.log('b='+b); //123
Reference data types are passed by reference: The reference address of the reference data type is saved in the stack, and the actual value is saved in the heap, and the reference address points to the space in the heap
Objects are stored in heap memory, and every time a new object is created, a new space will be opened up in heap memory
The variable holds the memory address of the object (reference to the object). If two variables hold the same object reference,
When one variable modifies an attribute, the other variable is also affected
var obj = new Object();
console.log(typeof obj);
0
When you compare the values of two basic data types, you compare the values
When comparing two reference data types, it is the memory address of the comparison object
It also returns false if the two objects are 1-mode 1 but have different addresses
var obj = new Object();
console.log(typeof obj);
1
10. Serialization of objects (turning objects into strings)
1) Normal conversion
obj.tostring()
2) Convert to an json string
JSON.stringify(obj)
3) Query string
var qs = require ('querystring'); //Introducing node. js Module
qs.stringify(obj) = > name=tom & age=12
11. Enhanced for Loop (for in)
Traversing objects
Syntax:
for (var variable in object) {}
for... There are several properties in the in statement object, and the loop body executes several timesOn each execution, 1 attribute name in the object is assigned to the variable
var obj = new Object();
console.log(typeof obj);
2