I have a potentially weird question about this
and jQuery plugins
As I understand it, the following is a very simple jQuery plugin:
$.fn.clickclone = function(param){ return this.click(function(){ param.apply(this); }); };
(pretending to be a plugin that somehow extends click()
.)
So, if I pass the function as an argument, it does what it needs and correctly calls this
as a DOM node. Easy.
This is all clear to me.
What is unclear is there a way to pass an argument to a non-function plugin and is it correct to access this
from the arguments? those. I could configure the plugin to do something like this:
$("#foo").pluginname(["foo", $(this).text() ]);
Such that for:
<a href="/bar" id="foo">Bar</a>
It would correctly pass the array to the plugin, and the second element in the array would return the value Bar
?
I do this mainly to provide syntactic sugar for my plugin, where you can pass the array as a shortcut (in addition to the usual callback function as the main function). In addition, I am losing access to using this
. Hence my dilemma.
EDIT: This is evil, but it seems like one job is to pass the argument as a string and then eval
. This is not an acceptable solution for me, but it illustrates what I would like to do:
$.fn.clickclone = function(param){ return this.click(function(){ if(typeof param === "function"){ param.apply(this); } else if(typeof param[1] === "string"){ console.dir("This is evil: " + eval(param[1])); } }); };