TypScript prototype mode pay attention to deep copy and shallow copy

//The essence of prototype mode is copy. Note that shallow copy or deep copy.

//Object is an instance containing a set of key-value pairs. The value can be a scalar, function, array, object, etc., as in the following examples:

let a = {

    a:1,

    b:2,

    c:(d:1), //Shallow copy, the address is still used

}

let b ={} ;

Object.assign( b, a ); //The core of the prototype mode is replication. 

YBLog.log( "Factory",a,a.c);//{a: 1, b: 2, c: {…}} {d: 1}

YBLog.log( "Factory",b,b.c);// {a: 1, b: 2, c: {…}} {d: 1}

a.c.d++;

a.b = 5;

YBLog.log( "Factory",a,a.c); //{a: 1, b: 5, c: {…}} {d: 2}

YBLog.log( "Factory",b,b.c);// {a: 1, b: 2, c: {…}} {d: 2}

Guess you like

Origin blog.csdn.net/ting100/article/details/108628688