【发布时间】:2012-10-27 00:22:09
【问题描述】:
根据这个 StackOverflow 答案What does jQuery.fn mean?,jQuery.fn.jquery 中的 fn 属性是原型属性的别名。我假设这在这两种方法中是相同的,其完整代码如下
$.fn.map = function() 和$.fn.tweets = function()
那么我的问题是,例如,如果 $.fn.tweets 使用原型来创建 tweets 方法,那么带有$('tweets').tweets 的代码是否会调用它...
var $tweets = $('#tweets').tweets({
query: buildQuery(approxLocation),
template: '#tweet-template'
});
如果是这样,它如何触发该方法。 例如,仅在文件加载时创建变量会触发该函数,该函数内部还有其他方法,即查询?感谢您的帮助
方法的完整代码
$.fn.map = function(method) {
console.trace();
console.log(method);
if (method == 'getInstance') {
console.log("fn.map");
return this.data('map');
}
return this.each(function() {
var $this = $(this);
var map = $this.data('map');
if (map && MyMap.prototype[method]) {
map[method] (Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
var options = method;
$this.data('map', new MyMap( this, options ));
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.map' );
}
});
}
$.fn.tweets = function(method) {
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.tweets' );
}
}
调用这些方法的变量?
var $tweets = $('#tweets').tweets({
query: buildQuery(approxLocation),
template: '#tweet-template'
});
var $map = $('#map').map({
initialLocation: approxLocation,
radius: 1000,
locationChanged: function(location) {
$tweets.tweets('setQuery', buildQuery(location));
}
});
【问题讨论】:
标签: javascript jquery