为什么几个JavaScript库将$用作一种或其他用途 [英] why does several javascript libraries use $ for one or other use

查看:50
本文介绍了为什么几个JavaScript库将$用作一种或其他用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过几个使用$的javascript库,包括jQuery,mootools,prototype等,甚至一些关于javascript的书都建议使用$代替document.getElementById的函数.只是一个随机的事情.

I have seen several javascript libraries using $ be it jQuery , mootools , prototype etc also even some of the books on javascript recommend using $ as a function replacement of document.getElementById. is it just a random thing.

推荐答案

在ECMA规范中实际上引入$作为有效变量名的原因是为了区分机器生成的代码和人工编写的代码.
大多数JS库使用$,因为很少要命名您的某些变量$,这样可以避免冲突和变量的意外覆盖.(但是现在由于每个库都在使用$,因此,如果您要使用两个库,那么您将陷入困境).

The reason $ was actually introduced as a valid variable name in the ECMA specs was to distinguish machine generated code from human written code .
Most JS libraries use $ because it is very rare that you would want to name some of your variable $ and hence it would avoid collision and accidental overwriting of the variable . ( However now since every library is using $ , if you want to use two libraries , you are in a soup ) .

这篇关于为什么几个JavaScript库将$用作一种或其他用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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