I have implemented several jQuery plugins for my current project.
Since some plugins have functions with the same name, one that is specified in the last defined one.
Here is the definition of my first plugin:
$(function($)
{
$.fn.initPlugin1 = function(parameters)
{
var defaultParameters = {};
$(this).data('parameters', $.extend(defaultParameters, parameters));
return $(this);
};
$.fn.function1 = function(){ console.log('Function 1.'); };
$.fn.callFunction = function(){ $(this).function1(); };
});
And here is the definition of my second plugin:
$(function($)
{
$.fn.initPlugin2 = function(parameters)
{
var defaultParameters = {};
$(this).data('parameters', $.extend(defaultParameters, parameters));
return $(this);
};
$.fn.function2 = function(){ console.log('Function 2.'); };
$.fn.callFunction = function(){ $(this).function2(); };
});
I have this scenario too:
$("#div1").initPlugin1().callFunction();
$("#div2").initPlugin2().callFunction();
In this particular scenario, the consoles show: Function 2. Function 2.
In fact, since it is callFunction()also defined in the second plugin, this is the one that is used.
I would like some to advise what is the best way to solve this problem. Is it possible to create something similar to a namespace?
source
share