Marcelo Camargo
Marcelo Camargo

Reputation: 2298

JS - Call to static method with prototype

Well, I searched and didn't find a way to call a prototype method without instantiate another object. I found how to do:

 var x = new X();
 X.MyMethod();

What I really need to know is if there is a way to do like in jQuery, doing something like:

var x = "mystring";
x.MyMethod();

In jQuery UI, we call .draggable(), .resizable() in a selected object. Can I do the same?

Upvotes: 0

Views: 357

Answers (1)

Shelby L Morris
Shelby L Morris

Reputation: 726

You want to extend the String's prototype methods? You'll need to do something like this:

String.prototype.MyMethod = function() {
    console.log("MyMethod()");
};

var x = "mystring";
x.MyMethod();

See also: How does JavaScript .prototype work?

Or if you want to be really clever, study: MDN Reference: Define Property

Upvotes: 1

Related Questions