I have already a short-hand function like so:
function myObj() {};
myObj.prototype.read = function (name) {alert(name);};
...(more functions)
Now I would like to "convert" this to a jQuery plugin.
What is the best way to do so? (My function doesn't need a selector before it).
I thought about doing it like this:
$.myObj.methodHere();
Thanks in advance.
Jonathan S.
5,8778 gold badges46 silver badges63 bronze badges
asked Apr 13, 2012 at 20:53
funerr
8,31417 gold badges91 silver badges139 bronze badges
-
1Why do you want to do this, out of interest? There's no real need to add the extra overhead if you don't need to.Jack Franklin– Jack Franklin2012年04月13日 20:59:11 +00:00Commented Apr 13, 2012 at 20:59
1 Answer 1
Just add your method to the jQuery object.
$.myObj = myObj;
Then you can call it like:
$.myObj.methodHere();
EDIT: Why do you want to do this? jQuery plugins are supposed to act upon jQuery objects. Your function "doesn't need a selector before it", therefore it's not really a "plugin".
answered Apr 13, 2012 at 20:57
gen_Eric
228k42 gold badges304 silver badges343 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
aknosis
Although pointless, $.myObj is no diff than calling myObj directly, this is the correct answer
funerr
when I try it firebug throwes this error: "$.myObj.read() is not a function"
gen_Eric
@agam360: That's because
read is part of the object's prototype, not the object itself.funerr
Ohh yea I forgot to add the prototype(for the inheretence) "$.myObj= myObj.prototype;" thanks!
lang-js