How to return object relative object?

2

I need to return a previous object from an object, this is because I am using this on function for tests.

See:

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...
    }
};

This this returns the object / property style of a prototype NodeList (I'm using querySelectorAll for the test, it generates some divs), but that was not the intention.

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

{background:null,backgroundColor:null}

Is there any way legal to return the relative object of the relative object of that object this , which comes before prototype ?

Note:

  • I want to do something like parentElement , but without DOM , using objetos / propriedades , such as:
  • Objeto={Huh:null,Propriedade:null};
  • Objeto.Propriedade.parent , I wanted to return Objeto .
asked by anonymous 14.01.2016 / 02:22

1 answer

2

If you are a relative, you mean parent (parent). You can use .parentNode or .parentElement . They work as follows:

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

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

So you catch the parent element 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 to h1 , and its parent to div , which receives the effect.

The examples I used are not the same as your case because the question is vague, and your code is not really a "javascript".

    
14.01.2016 / 03:14