DEV Community

Cover image for What is the purpose of this code?
Baba
Baba

Posted on

What is the purpose of this code?

Today I was playing with the Object.defineProperty method and doing weird stuffs like making the 3rd index of an Array to 🔥always🔥 return 'Frank Stallone' (don't ask why)

console.log([1,2,3,4][3]); // Frank Stallone

anyways

I added debugger statement to set method of a descriptor:

Object.defineProperty(Array.prototype, 3, { set() { debugger; } }); 
Enter fullscreen mode Exit fullscreen mode

after typing something to the console triggers debugger with 2 calls in the call stack:
image

Code snippet in the first call:

(function i(t) { let e; e = "string" === t ? new String("") : "number" === t ? new Number(0) : "bigint" === t ? Object(BigInt(0)) : "boolean" === t ? new Boolean(!1) : this; const s = []; try { for (let i = e; i; i = Object.getPrototypeOf(i)) { if (("array" === t || "typedarray" === t) && i === e && i.length > 9999) continue; const n = { items: [], title: void 0, __proto__: null }; try { "object" == typeof i && Object.prototype.hasOwnProperty.call(i, "constructor") && i.constructor && i.constructor.name && (n.title = i.constructor.name) } catch (t) {} s[s.length] = n; const o = Object.getOwnPropertyNames(i) , r = Array.isArray(i); for (let t = 0; t < o.length && n.items.length < 1e4; ++t) r && /^[0-9]/.test(o[t]) || (n.items[n.items.length] = o[t]) } } catch (t) {} return s } ) 
Enter fullscreen mode Exit fullscreen mode

Can someone explain to me what is the purpose of this code? and what it does?

Top comments (0)