2

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.

1
  • 1
    Why 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. Commented Apr 13, 2012 at 20:59

1 Answer 1

4

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".

Sign up to request clarification or add additional context in comments.

4 Comments

Although pointless, $.myObj is no diff than calling myObj directly, this is the correct answer
when I try it firebug throwes this error: "$.myObj.read() is not a function"
@agam360: That's because read is part of the object's prototype, not the object itself.
Ohh yea I forgot to add the prototype(for the inheretence) "$.myObj= myObj.prototype;" thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.