如何从外部覆盖以下jQuery示例插件代码的方法。
//This is simplified skeleton of infinatescroll plugin.
(function ($)
{
var defaults = {
prop: 'name'
};
var methods = {
init: function (params)
{
//Some code
return this;
},
retrive: function ()
{
console.log('Inside Plugin');
return this;
}
};
$.fn.my_plugin = function ()
{
//Some code
return methods.retrieve.apply(this, arguments);
};
})(jQuery);
我试图覆盖'retrive'方法,如下所示,但它不起作用。
(function ()
{
var originalPlugin=$.fn.my_plugin;
$.fn.my_plugin.retrive = function ()
{
console.log('Outsite Plugin');
};
originalPlugin.apply(this,arguments);
$('body').my_plugin();
})();
答案 0 :(得分:0)
<script type="text/javascript">
//This is simplified skeleton of infinatescroll plugin.
(function ($)
{
var defaults = {
prop: 'name'
};
var methods = {
init: function (params)
{
//Some code
return this;
},
retrive: function ()
{
console.log('Inside Plugin');
return this;
}
};
$.fn.getObj = function(){
return methods;
};
$.fn.my_plugin = function ()
{
//Some code
return methods.retrive.apply(this, arguments);
};
})(jQuery);
$(document).ready(function(){
var originalPlugin=$.fn.my_plugin;
console.log($.fn.getObj());
$.fn.getObj().retrive = function(){
console.log("look I am changed");
}
$('body').my_plugin();
});
</script>