I am in the midst of creating a recursive function for Get Object.getOwnPropertyDescriptors() from Javascript recursively, and flatten it .
This is the code:
let master = [];
function something (obj,str) {
if (Object(obj)===obj) {
pobj = Object.getOwnPropertyDescriptors(obj)
for (let kk in pobj) {
maststr = str;
//console.log(obj);
//console.log("line 8");
//console.log(kk);
if (master.includes(kk)||kk==="constructor"||kk==="globalThis"||kk==="global") { //circular reference // ||typeof obj[kk]==="function"
return;
} else {
maststr = str + "." + kk ;
console.log("line - 1st");
console.log(maststr);
console.log(kk);
console.log(pobj[kk]);
pobj[kk]["name"] = maststr;
master.push(pobj[kk]);
console.log("line - 2nd");
console.log(maststr);
if (maststr==="globalThis.Function.prototype.arguments"||maststr==="globalThis.Function.prototype.caller"
||maststr==="globalThis.Function.prototype.callee") continue;
something(obj[kk], maststr);
}
}
} else {
//master.push(obj);
return;
}
}
something(globalThis,"globalThis");
console.log(master);
It worked well until I reached the Object.assign property, which strangely became undefined, and threw the following error:
/Users/user/Documents/playground/playgroundtests_js/E16_2.js:19
pobj[kk]["name"] = maststr;
^
TypeError: Cannot set properties of undefined (setting 'name')
at something (/Users/user/Documents/playground/playgroundtests_js/E16_2.js:19:21)
at something (/Users/user/Documents/playground/playgroundtests_js/E16_2.js:25:4)
at Object.<anonymous> (/Users/user/Documents/playground/playgroundtests_js/E16_2.js:38:1)
at Module._compile (node:internal/modules/cjs/loader:1191:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1245:10)
at Module.load (node:internal/modules/cjs/loader:1069:32)
at Function.Module._load (node:internal/modules/cjs/loader:904:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:22:47
Indeed, when I console.log this property just before the error was thrown, it was undefined. But when I tested Object.getOwnPropertyDescriptors(Object.assign) on the node REPL, this returned an object.
Why is this so, and how do I solve this?
Aucun commentaire:
Enregistrer un commentaire