我理解在流行的JavaScript库(如jQuery和Prototype)中使用(单个)美元符号.我也理解PHP(变量变量)中双美元符号的重要性.Dean Edwards在他着名的addEvent() JavaScript函数中使用了双美元符号.这是一个包含使用双美元符号的除外:
function addEvent(element, type, handler) {
// assign each event handler a unique ID
if (!handler.$$guid) handler.$$guid = addEvent.guid++;
// create a hash table of event types for the element
if (!element.events) element.events = {};
// create a hash table of event handlers for each element/event pair
var handlers = element.events[type];
if (!handlers) {
handlers = element.events[type] = {};
// store the existing event handler (if there is one)
if (element["on" + type]) {
handlers[0] = element["on" + type];
}
}
// store the event handler in the hash table
handlers[handler.$$guid] = handler;
// assign a global event handler to do all the work
element["on" + type] = handleEvent;
};
// a counter used to create unique IDs
addEvent.guid = 1;
Run Code Online (Sandbox Code Playgroud)
根据我的阅读,美元符号在JavaScript中仅对代码生成工具具有正式意义,尽管由于美元符号在上述JavaScript库中的广泛使用,该标准在今天基本上被忽略.
任何人都可以对JavaScript中双美元符号的使用有所了解吗?
非常感谢!
Dea*_*rds 85
我写了这个函数.:)
$$前缀不再具有任何特殊意义.当包装工受欢迎时,我用了一个符号.Packer会使用此前缀缩短变量名称.如今,Packer会自动缩短变量名称,因此这种表示法是多余的.
简短的回答,$ sign是JavaScript中的有效标识符,所以你只是看着一堆丑陋的变量名.;)
bri*_*vis 22
实际上,它只是开发人员的命名约定.对于所有意图和目的而言," $$"可能也是" aa".
正如您所提到的,单个美元符号函数$()在各种JavaScript库中几乎成为标准."统治他们所有人的一个功能",是吗?但请记住,这本身并不是JS本身所固有的.它只是一个可以替换的函数名称document.getElementById().
所以,说到这一点,$$可能是任何事情.哎呀,你甚至可以有这样的功能:
function $$$$$$$$(){
//i've got dollar signs in my eyes!
}
Run Code Online (Sandbox Code Playgroud)
换句话说,它只是一个变量/函数名称 - 就像其他所有名称一样.
正如其他人所说,$$是一个任意的标识符.
JavaScript工具包MochiKit保留了一个由相同标识的函数来使用CSS选择器语法选择元素,就像jQuery一样,除非不将所有匹配元素包装在特殊对象中.
所以,是的 - 它本身并没有什么特别之处.
| 归档时间: |
|
| 查看次数: |
26435 次 |
| 最近记录: |