Is this in a Javascript class for builders?

1

Always use:

function Constructor() {
    this.algo = algo;
}

But really, what use this ? I only use it because I understand that that property, when placing the 'this', will be saying that the property is for that constructor . Is that so?

    
asked by Eduardo Sebastian 24.05.2017 в 04:41
source

4 answers

2

In javascript the reserved word or operator this refers to the current object, I can can be modified (see use of Function.prototype.bind() ) for other purposes.

It serves, as a general rule, to differentiate between variables of global and local scope, accessing that of the object.

I'll give you an example to show you the difference.

var variable = 'Esta es una variable global';
var objeto = {
  variable: 'Esta es una variable de objeto',
  prueba: 'Esto no debería funcionar sin "this"',
  Constructor: function() {
    var variable = 'Esta es una variable local';
    /* Primera ronda de pruebas */
    console.log('window.variable: ' + window.variable);
    console.log('this.variable: ' + this.variable);
    console.log('variable: ' + variable);
    /* Segunda ronda de pruebas */
    try {
      console.log('prueba: ' + prueba);
    } catch(e) {
      console.log(e.message);
    }
    console.log('this.prueba: ' + this.prueba);
  }
};
objeto.Constructor();

As you may have checked in the first round of tests, the global variables are defined in the scope of window , the local ones are accessed directly and in the case of the scope of the object, this must be used to access them.

In the second round of tests, an exception prueba is not defined is generated when we try to access an object variable without using this .

Depending on the strict mode or not and several areas its behavior is different, but as a general rule its use is widely extended in the scope of an object, but in the global context , outside of any function, this refers to the global object (whether in strict mode or not).

    
answered by 24.05.2017 / 10:38
source
2

Basically because in POO the reserved word this that is generally used in Java, JavaScript, php (which have a syntax that is very similar between them) refers to the same class and its attribute in your case. It's as if the class called itself and brought the attribute or the method.

Now why in the constructor from my point of view it would be because if the constructor expects a parameter and this is called equal to an attribute of the class do not confuse which variable is being called.

I'll show you with PHP:

Class Humano{
    private nombre;
    function __contruct__(nombre){
        this.nombre = nombre
    }
}

The Human class has an attribute named name and a parameter named name is expected in the constructor. At this point the this comes into play. Then with the this it is calling the attribute of the class, but not the attribute that the constructor expects.

    
answered by 24.05.2017 в 06:56
0

In this particular case this is not necessary. In any assignment of a class attribute if the word is not written the compiler will understand it anyway, that is by default the compiler will precede this to any assignment implicitly. In this case the use of this is simply a good practice for the program to be more human readable and the programmer does not have to "think" as a compiler. In fact if you try to write in the constructor something = something you see that the result is the same but is less friendly in the eyes.

    
answered by 24.05.2017 в 07:49
0

In JavaScript we use this as a shortcut as a reference to an object;

var Person = {
  name: 'Bill',
  lastname: 'Gates',
  getFullName: function () {
    return this.name + ' ' + this.lastname
  },
};

this is something that the JavaScript interpreter maintains while evaluating the JavaScript code, such as a special CPU record that contains a reference to an object.

It is very useful when you create classes either using prototypes or class

class Person {
  constructor() {
    super(); // super help us to call functions on an object's parent.
    this.name = 'Bill';
    this.lastname = 'Gates'
  }

  getFullName() {
    return this.name + ' ' + this.lastname;
  }
}
    
answered by 24.05.2017 в 08:20