I'm having a hard time finding __proto__ of global object "window"

307 views Asked by At

I'm currently working on Chrome.

console.log(window.__proto__.__proto__.__proto__);

console.log(window.__proto__.__proto__.__proto__ === EventTarget.prototype);

I've found that first code above returns EventTarget.prototype

This can be verified by the second code, which returns "true"

In addition, the following code also returns true:

console.log(EventTarget.prototype.__proto__ === Object.prototype);

However, problems arise when you track who the child of EventTarget.prototype is.

console.log(window.__proto__.__proto__);

Above code returns below.

WindowProperties {Symbol(Symbol.toStringTag) : "WindowProperties" .....}

When I try to track the constructor "WindowProperties()" or the object "WindowProperties.prototype",

console says

Uncaught ReferenceError: WindowProperties is not defined at :1:13

why is this happen?

2

There are 2 answers

4
T.J. Crowder On

Because there is no built-in global called WindowsProperties exposed to your code. Just because something exists and has a name doesn't mean that your code has access to it. For instance:

const example = (() => {
    return new class Something { };
})();
console.log(example.__proto__.constructor.name); // Something
console.log(Something); // ReferenceError: Something is not defined

Note: I've used __proto__ in the code above so it would closely mirror yours, but I recommend not using __proto__ in real code. Use Object.getPrototypeOf (and, if unavoidable, Object.setPrototypeOf) instead. __proto__ is a deprecated feature defined in browsers only, and not all objects have it (although nearly all do; it's defined by Object.prototype).


Side note: Your code got me interested in the lineage of window in Chrome, so I wrote the following. You may find it interesting:

const seenBefore = new Set();
let indent = "";
let obj = window;
while (obj) {
    const proto = Object.getPrototypeOf(obj);
    if (seenBefore.has(proto)) { // Paranoia
        console.log("Got something we've seen before; breaking the loop");
        break;
    }
    seenBefore.add(proto);
    console.log(`${indent}${proto?.constructor?.name}`);
    indent += "  ";
    obj = proto;
}

On Chrome that gives me:

Window
  WindowProperties
    EventTarget
      Object
        undefined

On Firefox that gives me something slightly different:

Window
  EventTarget
    EventTarget
      Object
        undefined
2
jvm_code On

if you write window._ proto _ @ console it shows