Cloning and inheritance of js objects

to implement convenient object inheritance, the code is as follows:

let obj = { //objjson
  "a": 1,
  "b": 2,
  "d": {
    "d": 4
  }
}

let obj1 = newObj(obj); //obj1obj
obj1.a = 3; //obj1obj
obj.c = 4; //objobj1
obj1.d.d = 6; //obj1obj,obj.d.d=4
obj.d.d = 5; //obj1.d.d=6,"obj1.d.d = 6;"obj1.d.d=5

newObj () is the solution to this question I hope the boss can give a solution or idea

tried ideas:
(abandon) original prototype inheritance: the attribute name and length of obj are unknown, so it is too troublesome to write. It may be that I am not completely independent of
(failure) obj1 deep cloning obj:obj1 and obj, and obj1 cannot be updated with obj;
(failure) obj1 is cloned deeply first and then opens a field to directly reference the part of the public address referenced by obj:. Obj1 will affect obj

.
Mar.20,2021

The

prototype chain is actually a special setter/getter , get prototype, and set own. So it is suitable to make this one-way change.

this is an anti-pattern, so don't use this hack too often! Normal code should only store functions on the prototype chain!

if you don't know what you're doing, don't think in that direction. It's a good thing that you can't think of something bad.


<strong></strong><br>objjson;<br>obj1objobjobj;<br>obj0obj;

let obj0 = {
//  'b': 33,
//  'd':{'d':88},
  'f': [4,5,6],
  'g':[{
    'g0': 7,
    'g1': 7,
  }]
};

function deepPrototypeClone(obj) { //obj
  const ret = Object.create(obj);
  for (const [key, value] of Object.entries(obj)) {
    if (value && typeof value === 'object') {
      ret[key] = deepPrototypeClone(value);
    }
  }
  return ret;
}
    
function updateObj(obj0,obj){ //objobj0
  for (const [key, value] of Object.entries(obj0)) {
    if(value && value instanceof Array){
      if(typeof value[0] === 'object'){
        for(let i=0; i<value.length; iPP){
          updateObj(value[i],obj[key][i]);
        } 
      }else{
        obj[key].splice(0,obj[key].length,...value) ;
      }
    }else{           
      obj[key] = value;
    }
  }
};

let obj1 = deepPrototypeClone(obj); //obj1obj
//obj1.b = 22;
//obj1.d.d = 99;
//obj1.f = [5,5,5];
//obj1.g[0].g0 = '8';
//obj1.h = [['7','5']];
updateObj(obj0,obj); //obj,obj1
console.log("obj");
console.log(obj.b); console.log(obj.d.d); 
console.log(obj.f); console.log(obj.g[0]); console.log(obj.h[0]);
console.log("obj1");
console.log(obj1.b); console.log(obj1.d.d); 
console.log(obj1.f); console.log(obj1.g[0]); console.log(obj1.h[0]);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e54da6-452f9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e54da6-452f9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?