mat*_*att 4 jquery jquery-plugins
我正在尝试在jquery插件中使用.proxy()方法.不知道发生了什么,但它不是在调用methods.strobe.我有以下示例代码:
(function($) {
var settings = {
}
var methods = {
init: function(options) {
alert('init fired');
$.proxy(methods.strobe,this);
return this;
},
destroy: function() {
},
strobe: function(){
alert('strobe fired');
},
show: function() {},
hide: function() {},
refresh: function() {}
};
$.fn.notify = 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.notify');
}
};
})(jQuery);
$().notify();
Run Code Online (Sandbox Code Playgroud)
我有这个jsfiddle进行测试:http://jsfiddle.net/CZqFW/
任何输入将不胜感激.
jQuery proxy() 返回一个函数,该函数用第二个上下文关闭第一个参数.
您可以调用返回的函数,它将立即执行.
$.proxy(methods.strobe,this)();
Run Code Online (Sandbox Code Playgroud)
它为您提供的唯一内容是替换this上下文methods.strobe().您可以使用javascript的call()函数来完成同样的事情:
methods.strobe.call(this);
Run Code Online (Sandbox Code Playgroud)
您的jQuery插件已设置strobe()为$ .fn.notify上的方法.所以你也可以这样称呼它:
this.notify('strobe');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2650 次 |
| 最近记录: |