JavaScript双美元符号 [英] JavaScript Double Dollar Sign

查看:188
本文介绍了JavaScript双美元符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解在流行的JavaScript库(如jQuery和Prototype)中使用(单个)美元符号。我也理解PHP(变量变量)中双美元符号的重要性。迪恩·爱德华兹在其着名的 addEvent()中使用双美元符号 JavaScript函数。这是包含使用双美元符号的除外:

I understand the use of the (single) dollar sign in popular JavaScript libraries such as jQuery and Prototype. I also understand the significance of the double dollar sign in PHP (variable variables). Dean Edwards uses the double dollar sign in his famous addEvent() JavaScript function. Here is an except containing the use of the double dollar sign:

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库中广泛使用。

From what I've read, the dollar sign only officially has significance in JavaScript for code generation tools, although that standard is largely ignored today due to the dollar signs widespread usage in the aforementioned JavaScript libraries.

任何人都可以了解JavaScript中双美元符号的使用情况吗?

Can anyone shed any light on this usage of the double dollar sign in JavaScript?

非常感谢!

推荐答案

我写了这个函数。 :)

I wrote this function. :)

$$前缀不再有任何特殊意义。当包装工受欢迎时,我用了一个符号。 Packer会使用此前缀缩短变量名称。现在Packer将自动缩短变量名称,因此这种表示法是多余的。

There is no longer any special significance to the $$ prefixes. It was a notation that I used back when packer was popular. Packer would shorten variable names with this prefix. Nowadays Packer will automatically shorten variable names so this notation is redundant.

简短回答,$符号是JavaScript中的有效标识符所以你只是看着一堆丑陋的变量名。 ;)

Short answer, the $ sign is a valid identifier in JavaScript so you are just looking at a bunch of ugly variable names. ;)

这篇关于JavaScript双美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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