2
How can I rename objects, properties and methods to the names I want, thus replacing the old ones that would cease to exist. For example, I tried the following code:
documento = document;
documento.corpo = documento.body;
documento.corpo.estilo = documento.body.style;
documento.corpo.estilo.fundo = documento.body.style.background;
delete document;
document.body.style.background = "#141414"; // funciona mas não devia
documento.body.style.background = "#141414"; // funciona mas não devia
documento.corpo.estilo.fundo = "#141414"; // não funciona mas devia
One of its uses would be to change the language of Javascript but there are also other possibilities.
Edited:
I figured out how to change the name of the property, but I still don’t know if it’s possible to delete the original. Maybe it can’t be because it’s essential, even though I made an identical copy.
documento = document;
documento.corpo = documento.body;
documento.corpo.estilo = documento.corpo.style;
// aparentemente delete não está a fazer nada
delete document;
delete document.body;
delete document.body.style;
delete document.corpo.style;
documento.corpo.style.background = "#141414"; // funciona
documento.corpo.style.background = "#141414"; // funciona
documento.corpo.estilo.background = "#141414"; // funciona
Just don’t do it. Chances are you won’t be able to use any external lib in your project if you do.
– Giovane
@Giovane, I understand what you mean but I’m curious whether or not you give to do and how. I also never use anything external so there would be no problem in that part.
– user7393973
You can even make a copy of this type of object, but that’s not what your code is doing (it’s referencing, not copying). Now, delete objects like
window
anddocument
probably not even possible, and if it were would cause serious problems to the browser.– bfavaretto
Really, erase
document
orwindow
it is impossible because both have the property attribute[[Configurable]]
defined asfalse
. Thus, erasing them becomes impossible (so much so that, in strict mode, we usedelete
there would cause an error). Related: What and how Javascript property descriptors and attributes work?– Luiz Felipe
However, you could use the object
Proxy
to create a new objectdocument
, newdocument.style
and related, mapping them to the behavior of the originals. However, I don’t really know what would be the motivation for this. The work would be huge for something really unnecessary... :/– Luiz Felipe