我理解在流行的JavaScript库(如jQuery和Prototype)中使用(单)美元符号。我也理解PHP中双美元符号的意义(变量变量)。 Dean Edwards在他着名的addEvent() JavaScript函数中使用双美元符号。下面是一个除包含使用双美元符号: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;
从我读过,美元符号才正式拥有的JavaScript代码生成工具的意义,虽然该标准在今天很大程度上被忽略了由于美元在前面提到的JavaScript库中有广泛的用途。
任何人都可以在JavaScript中使用双美元符号吗?
非常感谢!
在我看来,这'addEvent'将泄漏就像在IE筛子。 – kangax 2009-09-23 04:32:09