Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
ES6 Proxies for location.pathname changes

Is it possible to use ES6 Proxies to track changes of location.pathname?

As far as I know, Proxies can be used for objects, and location.pathname is an object, right?

If the answer is yes, can someone help me understand how to achieve this?

The only working thing I managed to create is the following code:

const handler = {
  get(target, key) {
    console.log(`Reading value from ${key}`)
    return target[key];
  },
};

const p = new Proxy(location, handler);
console.log(p.pathname);

But the above code will only show once the current URL is in the browser's tab.

How can I make it work to keep tracking the changes?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

window.location is a nonconfigurable getter/setter, which means that it can't be replaced:

console.log(Object.getOwnPropertyDescriptor(window, 'location'));

The same is true for the .pathname property of the object returned by the getter:

console.log(Object.getOwnPropertyDescriptor(window.location, 'pathname'));

And a location variable can't be declared on the top level:

const location = { newObject: 'foo' };

Which means that there's no way to intercept changes to it by replacing it with a Proxy - any references or assignments in the existing code to window.location.pathname will necessarily invoke the browser's built-in implementation.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda