【发布时间】:2011-11-05 16:14:56
【问题描述】:
我需要构建一个只有几千字节大小的小型 JavaScript 库。为此,我想使用一些 jQuery 非常有名的设计选择。
以下是我目前拥有的两个外壳,我想知道是否有人可以建议我哪种样式可能是更好的设计选择。
var jQuery, $;
(function() {
jQuery = $ = function(selector, context)
{
return new JQuery(selector, context);
};
var JQuery = function(selector, context)
{
// ...
return this;
};
jQuery.fn = JQuery.prototype = {
example: function()
{
//...
return this;
}
};
}());
还有一个稍微修改过的 jQuery shell 版本。
(function(window, undefined)
{
var jQuery = function(selector, context)
{
return new jQuery.fn.init(selector, context);
};
jQuery.fn = jQuery.prototype = {
init: function(selector, context)
{
// ...
return this;
},
example: function()
{
//...
return this;
}
}
jQuery.fn.init.prototype = jQuery.fn;
window.jQuery = window.$ = jQuery;
})(window);
我还想知道我是否对其中任何一个做出了错误的设计选择。 JavaScript 不是我的主要语言,所以我想确保我没有做错任何事情。
【问题讨论】:
标签: jquery javascript