Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

372
Visualizações
How to check if JavaScript object was created in the current window or another window?

Let's say I have an object OtherObj that was created in a window OtherWindow which is different from the current one ThisWindow:

const ThisWindow = window;
const ThisObj = ThisWindow.history;

const OtherWindow = window.open();
const OtherObj = OtherWindow.history;

console.log(ThisObj instanceof Object); //true
console.log(OtherObj instanceof Object); //false
console.log(OtherObj instanceof OtherWindow.Object); //true, but this method only if I already have a reference to OtherWindow

Now imagine if I only have a reference to OtherObj, is there a way to get the window that was used to create it? Maybe there is a property on OtherObj that holds a reference to the window it was created in?

I'm currently trying to come up with a cross-window way to use the instanceof operator. As you can see in the code example, [variable] instanceof Object will return false if the variable is pointing to an object created outside of the current window.

Some of you might say to just use OtherObj instanceof OtherWindow.Object (which returns true), but that only works if I already have a reference to OtherWindow. My question is assuming I do not already have a reference to OtherWindow.

Is there a property somewhere on OtherObj that points to the window that created it?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Short answer:

const OtherWindow = OtherObj.constructor.constructor('return window')();

Long answer:

Because most things are an object in JavaScript you can access their constructor with Object.getPrototypeOf(ObjectIn).constructor, from the objects prototype. But because of the prototype chain ObjectIn.constructor has the same effect.
And because that constructor is a function, its constructor is Function. And once we have Function we can access everything from that window with Function('return ...')().

So an implementation to get Function could be as follows:

const getFunction = (ObjectIn) => {
  // Get ObjectIn's constructor
  const ObjectInConstructor = ObjectIn.constructor;
  // Get Function
  return ObjectInConstructor.constructor;
};

const getObject = (ObjectIn) => {
  const Function = getFunction(ObjectIn);
  return Function('return Object')();
};

const OtherWindowObject = getObject(OtherObj);
const OtherWindow = getFunction(OtherObj)('return window')();

Because StackOverflow's HTTP headers don't allow open() I've created a fiddle to demonstrate.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda