I’ve been playing with jQuery lately and I need now a function for strings, and I want to execute it like this:
var str = "mystring";
if( str.myPlugin( ) )
{
}
But all the examples I found for jquery plugins can only be used like $(selector).myPlugin();If I apply them to a string i get “… is not a function”.
How can I do this?
When you create a string, what you are really creating is a new instance of the
Stringobject.You can add methods to all instances of the object using the object’s prototype.
Adding new methods to native objects such as the
Stringobject is usually frowned upon due to possible name conflicts.Edit: just to add clarification.
This is not a method of jQuery and jQuery is not required to create these kinds of methods.