关于js 中的 $()

并不是js的什么特殊用法。其实只是你的页面引用到的js(或被include页面的js)文件中定义有 名为$的这样一个function而已。

就我本人来说,开始在一个页面中看到这样一种用法:

后来终于在这个页面include的另一个页面包含的prototype.js中发现定义有这样一个名为$的function。
附prototype.js中 $() 的定义:

function $(element) {
if (arguments.length > 1) {
for (var i = 0, elements = [], length = arguments.length; i < length; i++)
elements.push($(arguments[i]));
return elements;
}

if (typeof element == 'string')
element = document.getElementById(element);
return Element.extend(element);
}



参考:
[url]http://www.javascriptsearch.com/guides/Advanced/articles/0607ABetterDollarFunction.html[/url]
[url]http://osric.com/chris/accidental-developer/2008/04/the-javascript-dollar-sign-function/[/url]
[url]http://www.phpguru.org/article/the-javascript-dollar-function[/url]
[url]http://javascript.internet.com/snippets/prototype-dollar-function.html[/url]

[b]The dollar function must die[/b]
[url]http://blog.dorward.me.uk/2009/02/19/the-dollar-function-must-die.html[/url]
[quote]
A number of JavaScript libraries have a function called $ and I’m irritated by it for a number of reasons:

The dollar sign is intended for use only in mechanically generated code.

… which the libraries are not.

The name doesn’t give a clue as to what the function does or where maintainers should look for documentation.

Different libraries use the $ function to do different things. In Prototype.js it means “Get the element with this id”, but in jQuery is means “Get all the elements that match this CSS selector”. Maintainers have to know what library is in use to know what the function does and can’t easily tell by just looking at the code.

If you have a library with a $ function, please don’t use it. Good libraries have alternatives (such as jQuery) or don’t create the function at all (such as YUI).
[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值