0

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?

Aaron Dufour
  • 17,288
  • 1
  • 47
  • 69
Marcelo Camargo
  • 2,240
  • 2
  • 22
  • 51

1 Answers1

1

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

Community
  • 1
  • 1
Shelby L Morris
  • 726
  • 5
  • 10
  • Would be there a way to I get and manipulate the content of the passed string in my lambda function? – Marcelo Camargo Jul 12 '14 at 04:04
  • Yes. Inside of that function, the variable `this` will return the value of the `string` and you can manipulate it as you wish – Shelby L Morris Jul 12 '14 at 04:06
  • 2
    @sxnine - strings are immutable so you cannot change the current string object in a method. You can create a new string and return that from a method, but the current string object can't be changed. – jfriend00 Jul 12 '14 at 04:26
  • right on, i should've been more clear. it'll throw an error if you try to mutate `this`. good call. – Shelby L Morris Jul 12 '14 at 04:45