How to return object relative object?

Asked

Viewed 87 times

2

I’m needing to return a previous object from an object, that’s because I’m using this test function.

Behold:

NodeList.prototype.style={
    set background(a){
        DefStyle("background",a,this)//aqui
    },set backgroundColor(a){
        DefStyle("backgroundColor",a,this/*não era isso que eu queria :/*/)//e aqui...
    }
};

That one this returns the object/property style prototype NodeList (I’m using querySelectorAll for the test, it generates a Divs), but that was not the intention.

The this currently return an object with setters background and backgroundColor! Example (without using setters):

{background:null,backgroundColor:null}

Is there any way legal return the parent object of the parent object of that object this, come before prototype (always know there don’t seem to be ways)?

Obs:

  • I want to do something like parentElement, but without DOM, using objetos/propriedades, as:
  • Objeto={Huh:null,Propriedade:null};
  • Objeto.Propriedade.parent, I wanted to return Objeto.
  • 1

    I read your question three times and I don’t understand what you’re trying to do. Your question is (or seems to be) something simple, but you express yourself in such a confused way that you cannot understand anything. The first suggestion is as to that name "relative". If the idea is to translate the term "Parent" of English, so you did wrong, because "Parent" is not "relative" in English. In English the term "Parent" It is a word referring to a person who is a father or mother. However, it is still hard to understand. I suggest you give more details about what you are trying to do with this.

  • Do you refer to elements of the DOM or do you want to know from which object another object has inherited?

  • @Victorstafusa I know that, but what I want to do is something like parentElement, only with objects/properties, in JS, by this on my prototype. :/

  • @Sergio I mean the one NodeList! I want to pick up the relative object until such return querySelectorAll with more Ivs. Note: Everything that returns querySelectorAll sane prototype of NodeList, and are something like arrays, although.

  • Sorry for the delay, my mother sent me to sleep.......

  • 1

    Obey and honor your mother. Good sleep.

  • @durtto And good morning, dss.

  • Good morning. Look at the answers, I think they got you a solution.

  • @durtto The only problem is because the solutions have to do with DOM.

  • I’m sorry but it’s still unclear to me. Do you want to write the prototype with a Seter/getter your? that allows you to write/read the style and also return the parent element an/Node element?

  • I want to apply a style to several elements within a NodeList (querySelectorAll), but without looping in line, with the same face of the JS, to do some tests... as: document.querySelectorAll(".lF").style.huh=..., and for that I need to take the object parente of this in prototype that I posed in the question.

Show 6 more comments

1 answer

2

If by "relative," you mean Parent (father). You can use the .parentNode or the .parentElement. They work as follows:

<div>
  <h1 id="myTitle" >Title</h1>
</div>

document.getElementById("myTitle").parentElement.nodeName // isso retorna "DIV"

So you capture the element father of the element in question. Look below:

Example

HTMLElement.prototype.backgroundColor = function(color){
   this.parentNode.style.backgroundColor = color
};
document.querySelectorAll('h1')[0].backgroundColor('gold');
<div>
  <h1>Title</h1>
</div>

In the above example I apply the function backgroundColor at the h1, and your Parent to div, that receives the effect.

The examples I used are not the same as in your case due to the question being vague, besides that your code is not really a "javascript".

  • parentNode does not work for objetos, huh!

Browser other questions tagged

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