Renaming objects, properties and methods in Javascript

Asked

Viewed 229 times

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, 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.

  • 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 and document probably not even possible, and if it were would cause serious problems to the browser.

  • 1

    Really, erase document or window it is impossible because both have the property attribute [[Configurable]] defined as false. Thus, erasing them becomes impossible (so much so that, in strict mode, we use delete there would cause an error). Related: What and how Javascript property descriptors and attributes work?

  • 1

    However, you could use the object Proxy to create a new object document, new document.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... :/

1 answer

2

It doesn’t work because background is only a property of style, that in this hierarchy string is the last object that can be referenced. I don’t see any productivity or practical application for this substitution, but for the purpose of understanding the discussion may have some validity.

When you do that:

let documento = document;
documento.corpo = documento.body;
documento.corpo.estilo = documento.body.style;
documento.corpo.estilo.fundo = documento.body.style.background;

Are you just assigning the value of background à fundo. Despite estilo be a reference of style, fundo is just an attribute added by you and that the browser is not looking to set the background color as it keeps looking for background.

Again, it makes no sense that you want to rename objects and properties of DOM, because what you’re doing is just "clone".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.