0

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?

kwonryul
  • 481
  • 3
  • 10

2 Answers2

2

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
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • wow... it's awesome... Is there any way in which I get an access to the origin of "window"? – kwonryul Jun 13 '20 at 14:44
  • Google seems to be hiding native-code related to built-in objects. Can't I get an access? – kwonryul Jun 13 '20 at 14:46
  • 1
    @kwonryul - I don't know what you mean by "the origin of window." FWIW, Chrome is built on the Chromium project, which is open source, so it's always possible to get into the details if you like... :-) – T.J. Crowder Jun 13 '20 at 14:49
  • 1
    It really helped me a lot. Have a nice day! :) – kwonryul Jun 13 '20 at 15:15
0

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

jvm_code
  • 33
  • 1
  • 1
  • 8
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 20 '21 at 09:47
  • thats good could have added few more things like there is https://developer.mozilla.org/ for reference – jvm_code Sep 22 '21 at 05:14