What’s the difference of using function with THIS or OBJ parameter?

Asked

Viewed 67 times

3

Making a Javascript code recently, I found that when using a Function, when I put THIS as a parameter it was not executed, but when putting OBJ, it worked normally. Can someone tell me the difference between the two?

function mostraCampo(obj) {
          var select = document.getElementById('instituição')
          if (select.value == 'OUTRA') {
            document.getElementById("outrainst").style.visibility = "visible";
          } else{
            document.getElementById("outrainst").style.visibility = "hidden";
          }
        }
<div class="form-group">                
          <label> Instituição de ensino <br />
            <select class="form-group" name="instituição" id="instituição" onchange="mostraCampo(this.value);">
              <option value="UFTM">UFTM</option>
              <option value="UNIUBE">UNIUBE</option>
              <option value="FACTHUS">FACTHUS</option>
              <option value="SENAI">FAZU</option>
              <option value="IMEPAC">IMEPAC</option>
              <option value="NENHUMA">NENHUMA</option>
              <option value="OUTRA">OUTRA</option>
            </select>
            <input type="text" class="form-control" name="outrainst" id="outrainst" style="visibility: hidden;">
          </label>
        </div>
  • PHP code or Javascript? By the way, which code? What is this and what is OBJ in this example? Please edit your question and be clearer on the problem, especially by adding the code that generated the doubt.

  • It’s edited, I forgot to mention that are the parameters of Function! And I didn’t know how to put the code.

  • 1

    In advance: this is an object that represents the current element, already obj, in this case, it is the argument of the function. They are very different things. See if this helps you: https://answall.com/questions/233628/como-utili-o-this-maneira-correct

  • I think I understood, for example, to use this as I used to call the function within the right select because it will refer to select, but to use this as an argument of the function it will understand this as a variable would be this?

  • Basically this, so much so that in this case obj will be the this.value of select, then, in the job, all you had to do was if (obj == "OUTRA") ..., without needing in the var select.

  • Got it, thank you very much!!!

Show 1 more comment

1 answer

1

Well, if I understand your question is because this is reserved by the parser, then you can’t use it as a function parameter, because it can be used inside it to set information in the function. You can put the name you want in fact, except those that are reserved by the system. Obj is more a convention.

this serves as an example:

function Pessoa(){
  this.idade = 0;

  setInterval(() => {
    this.idade++; // propriedade |this|refere ao objeto pessoa
  }, 1000);
}

So this is related to when you want to call the function itself. The obj is when you need to enter a parameter in the function, by convention, uses obj, but it could be Xuxa that would work. It just can’t be a reserved word.

  • Give a fix on the answer, to make it perfect. And I believe his question refers to the difference between THIS and OBJ

  • got better now, I even gave a practical example

Browser other questions tagged

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