What is the parameter THIS? [duplicate]

2

Something I always see in many code out there is the this parameter.

Ex:

$(this).funçao-variável...

I've already been told that this serves to "reference" something. But it was not very clear.

Does anyone know what the utility / what is this ?

    
asked by anonymous 01.03.2016 / 18:15

2 answers

2

In JQuery, $ (this) refers to the element you are currently using.

For example, if you created a jQuery blur function for a particular text field, instead of referring to the field every time you use it, you can simply use $ (this) within the context of the element.

$('#field').blur(function(){
    var field_value = $(this).val(); // $(this).val() se refere ao valor de $('#field')
    alert(field_value);
});

I hope it has now become clear.

    
01.03.2016 / 19:16
2

This simple article explains the use of the terminology.

Every JavaScript function, when executed, generates an association of the object created by the interpreter through the reserved word this. The ECMAScript specification calls this ThisBinding, an event that happens every time a JavaScript code is executed and a new execution context is established. The value of this is constant and it exists as long as this execution context exists.

Read more at Learn more about "this" used javascript

I hope I have helped.

    
01.03.2016 / 18:50