5

我只是在为我正在开发的应用程序开发自己的迷你框架,并且我一直在研究 jQuery 的编码。

我明白了 $(selector).function() 的工作方式,但是你为什么可以调用一些函数,例如:

$.ajax()

这肯定是美元符号同时引用了一个函数和 jquery.fn 对象吗?

提前致谢!

4

2 回答 2

8

Functions are objects in JavaScript so they can have properties.

$ is the jQuery object, when using $() it is used as a constructor (it contains some magic so new is not necessary); but it also contains lots of methods (and some non-callable properties such as $.browser) available as $.something

于 2012-06-18T18:04:08.230 回答
5

http://jsfiddle.net/vZvgv/1/

var $ = function(str) {
    document.write(str+'<br />');
}

$.ajax = function(str) {
    document.write(str+'<br />');
}

$.answer = 42;

$('dollar');
$.ajax('ajax');
document.write($.answer);
于 2012-06-18T18:15:47.687 回答