Chapter III - I object

object

var o={a:1,b:2};
o.a  o Object a attribute
o.a=Value sets the value of the property

The structural form of an object is a key value pair( key)  Value( value)  Pair
a Key 1 value
 Duplicate keys cannot appear in key value pairs
var o={a:1,a:10} error
 In an object, the key can only be string perhaps symbol


var b=false;
    o.b=10;there o.b  this b Not a variable b It's a string b

    o The key under the object is a variable b Value of
    o[b]=10;
    var a="c";
    var b="d";
    o[a][b]=100;
  var obj={};

obj.a=10; Set an a attribute under obj. The value of this attribute is 10
obj[“a”]=10; Set an a attribute under obj. The value of this attribute is 10
Point syntax requires attribute names, which must be similar to the naming convention of variables. Values cannot be written directly, and illegal symbols
However, if the key of the object is a string, various characters are allowed. If there are other character keys, we use [key] to write

The "" string is not used when the key is used. At this time, the content given is considered as a variable, that is, the value of the variable is used as the key

 var a=10;
    obj[a]=10;
    obj={"10":10};
	obj["a"]={};

    obj["a"]={};
    obj["a"]["b"]=2;  // {a:{b:2}}

When any content is used as a key, it will be automatically converted to a string
var a;
var b;
The key attribute of the object is to convert the value of this variable into a string. The value of the a variable is undefined, which will become "undefined" when converted to a string
o[a]=10—>o[“undefined”]=10; The variable does not use '', so the variable takes the value as the key here
o[“a”]=20;—> o[“a”]=20; All strings used are strings
Print o[b] first convert B to the string "undefined" to get the attribute "undefined" in object o in the object
The value of "undefined" just set is 10, so the result is 10

-Traversal of objects

   var obj={a:1,b:2,c:3,d:4,e:5};
    for  in    Traverse all of the objects key
    for(var key in obj){
   
    }

In other languages, the traversal of objects is disorderly, and in js, the traversal of objects is regular
All keys are numeric first, traversing from small to large, and then traversing other keys in the order of creation.

-Storage of objects

var o={a:1,b:2};
Why do you want to be an object.
var a=1;
var b=2;
var c=3;
Multiple spaces need to be opened in the stack and cannot be classified
Stack is equivalent to memory and heap is equivalent to hard disk
Stack speed is fast, heap speed is slow, stack space is small, heap space is large
Classified storage, there are objects
{id:10001,name:"mac pro",price:20000,num:1},
{id:10002,name:"iphone 13 pro",price:8000,num:2},
Exists in the heap and generates a reference address in the heap

-Reference address assignment

The following is the assignment of reference address. The results of obj and obj1 are the same reference address

 var obj={a:1,b:2};
 var obj1=obj;
 
  obj.a=2;  Modify the object corresponding to the reference address
  obj={a:2,b:2};   Reference address modified
 
 
   var obj={a:1,b:2};
 
 
     function fn(o){
         o.a=10;
     }
     fn(obj);
 
     console.log(obj);
 
 
      function fn(o){
         o={a:10};
      }
 
     fn(obj);
     console.log(obj);

-Copy object

Shallow replication

var obj={a:1,b:2,c:3};
var obj1={};
// obj1.a=obj.a;
for(var key in obj){
    obj1[key]=obj[key];
}
obj.a=10;
console.log(obj1,obj);

Deep replication

function cloneObj(targetObj,sourceObj){
            for(var key in sourceObj){
                if(typeof sourceObj[key]==="object" && sourceObj[key]!==null){
                    var o={};
                    targetObj[key]=o;
                    cloneObj(o,sourceObj[key])
                }else{
                    targetObj[key]=sourceObj[key];
                }
            }
        }

JSON

JSON.stringify(obj) Convert objects to JSON character string
JSON.parse(str)  take JSON Convert string to object
 error
    Unexpected token d in JSON at position 22 JSON Error in string
    Unexpected token b in JSON at position 0 

This also allows deep replication

 var o=JSON.parse(JSON.stringify(obj));

Delete attribute

var obj={a:1,b:2};
delete obj.a;

Object deletion

null  Cut off the reference relationship and clear the content corresponding to the reference list
var obj={a:1,b:2};
 var obj1=obj;
 obj=null;
 object{a:1,b:2} The referenced variable is only obj1,If you want to completely clear this object
 obj1=null;

because null The main purpose is to cut off the reference relationship for reference types, so it is mainly used for reference types
 When the original variable type, the reference type is object Type, now the reference relationship is cut off, and the type of variable remains unchanged object

Memory leak

New orphan objects are generated continuously, but their reference relationships are not cleared, which will lead to the continuous increase of reference data in the heap
 Then it takes up a lot of heap space, resulting in new data, reducing the running space and slowing down, which is a memory leak

Orphan object

 The object still exists in the heap, but its reference address points to a variable and the variable points to other objects

garbage collection

When the memory rises to a certain value, JavaScript The virtual machine will automatically generate a purge, which will
 All objects without reference addresses are cleared to free up space in the stack

Keywords: Javascript

Added by dannyluked on Mon, 13 Dec 2021 10:13:31 +0200