我需要构建一个只有几千字节大小的小型JavaScript库。为此,我想使用一些jQuery让它如此出名的设计选择。
下面是我到目前为止拥有的两个shell,我想知道是否有人可以建议我哪种风格可能是更好的设计选择。
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外壳版本。
(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不是我的主要语言,所以我想确保我没有做错任何事情。
发布于 2011-11-06 01:27:33
jQuery (实际上不是jQuery )首先是一个非常糟糕的设计选择。window前缀,这对可读性和严格模式不好第二个示例中的jQuery.prototype似乎没有意义https://stackoverflow.com/questions/8021429
复制相似问题