我= JavaScript中的一种支持缩小的好习惯吗? [英] Is me = this in JavaScript a good practice to support minification?

查看:71
本文介绍了我= JavaScript中的一种支持缩小的好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我的JavaScript代码库中,我一直在使用var me = this习惯用法来支持缩小.但是我最近开始对此提出疑问,我想知道是否可以使用更智能的缩小工具来避免将this分配给变量?

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?

似乎工具应该能够检测函数,其中创建表示this的变量将节省多次调用this的字符.对于布尔值,例如var yes = true, nope = false,也可以这样做.

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.

是否可以使用一些工具来避免在我的所有代码库中设置var me = this?总的来说,更改代码库以更好地支持最小化是一个好主意吗?

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?

请记住,我确实知道,在某些情况下,我需要分配给this的变量来引用JavaScript闭包内的外部范围.我正在使用的var me = this仅用于支持缩小.

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.

推荐答案

在准备好实时部署代码之前,您不必担心缩小问题.我建议您在完成后使用Google的闭包编译器来缩小所有代码.在此处进行检查: http://code.google.com/p/closure-compiler/

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/

这篇关于我= JavaScript中的一种支持缩小的好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆