在 "学习JavaScript设计模式" 这本书的第 109 页,有一个代码示例让我感到困惑。
使用该函数的方法如下:
jQuery.single = (function( o ){
var collection = jQuery([1]); // <-- i want to ask this line
return function( element) {
// give collection the element
collection[0] = element;
// return the collection
return collection;
}
})();
使用该函数的方法如下:
$('div').on('click', function() {
var html = jQuery.single( this ).next().html();
console.log( html );
});
更新: 感谢您的回答。我从作者页面上查看了原始代码源 76 bytes for faster jQuery
var collection = jQuery([1]); // Fill with 1 item, to make sure length === 1
现在我明白了。我希望《学习JavaScript设计模式》的作者在引用这段代码示例时也能添加这个注释。
[1]
传递给函数! - AllTooSir$([1])
的意义是什么,而只有这个[1]
呢?” - Brewal