Is me = this in JavaScript a good practice to support minification?

无人久伴 提交于 2019-12-10 22:34:06

问题


So in my JavaScript codebase I have been using the var me = this idiom to support minification. But I've started questioning this lately, and I was wonder if assignment of this to a variable could be avoided with smarter minification tools?

It seems like a tool should be able to detect functions where creating a variable to represent this would save characters over multiple calls to this. The same could be done for boolean values, like var yes = true, nope = false.

Are there tools I could use to avoid setting var me = this all over my code base? In general is a good idea to change your codebase to better support minification?

Keep in mind that I do understand that in certain instances I need a variable assigned to this to reference outer scope inside a JavaScript closure. The use of var me = this I am taking about is solely for supporting minification.


回答1:


You should not worry about minification until you are ready to deploy your code live. I would recommend using Google's closure compiler to minify all of your code once you are done. Check it out here: http://code.google.com/p/closure-compiler/




回答2:


It doesn't just support minification, it can also be used in closures. When you're creating functions, for example to use in a callback, you have to take care of what "this" is when the function is called. A common mistake when starting out in Javascript is to get the wrong 'this'. The "var me = this" pattern helps to avoid that. For example

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

Note that frameworks like Ext discourage this and have you pass the scope variable along with the callback.

callSomethingAsynchronous(42, me.handlerFunction, me);

It allows you do leave the definition of the handlerFunction on the prototype rather than keep creating them. Sometimes a closure is just the better way because you have to enclose information that is only known at runtime

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

I use "var me = this" whenever I'm accessing this more than once in a function. The minifier does reduce it to a single character. If I'm accessing a child object I'll create a var for that too

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

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

   me.excerciseCount++;
}

I've seen code with huge nested accesses typed out (or cut and pasted) again and again and again, which cannot be minified, may cost a little performance and is harder to read and maintain.

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


来源:https://stackoverflow.com/questions/14739306/is-me-this-in-javascript-a-good-practice-to-support-minification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!