Is this = this in JavaScript good practice to support minimization?

So, in my JavaScript code base, I used idiom var me = thisto support mini programming. But lately, I started asking about it, and I was wondering, is it possible to avoid assigning a variable to a thisvariable using more intelligent minimization tools?

It seems that the tool should be able to detect functions where creating a variable for the view thiswill save characters across multiple calls to this. The same can be done for boolean values, for example var yes = true, nope = false.

Are there any tools that I could use to avoid installation var me = thisthroughout my code base? All in all, is it a good idea to change your code base to better support minimization?

Keep in mind that I understand that in some cases I need a variable assigned thisto refer to an external area inside JavaScript closure. The use var me = thisthat I accept is intended solely to support minimization.

+2
source share
2 answers
+1

, . , , , , "this", . Javascript 'this'. "var me = this" .

function doSomething() {
    var me = this;
    callSomethingAsynchronous(42, function (result) {
        // Here 'this' will depend on the caller of the callback functon.
        me.theResult = result;
    });
}

, , Ext, , .

callSomethingAsynchronous(42, me.handlerFunction, me);

handlerFunction , . - , ,

function sayHelloInAContrivedWay (name) {
    getTheGreetingAsync(function (greeting) {
        console.log(greeting + name);
    });
}

"var me = this" , . minifier . , var

function keepInnerWombatFit () {
   var me = this,
       wombat = me.inner.wombat;

   wombat.run();
   wombat.jump();
   wombat.sitDown();

   me.excerciseCount++;
}

, , ( ) , , , .

this.inner.wombat.run();
this.inner.wombat.jump();
this.inner.wombat.sitDown();
+1

Source: https://habr.com/ru/post/1542060/


All Articles