JavaScript prototype(ing) question

Posted by OneNerd on Stack Overflow See other posts from Stack Overflow or by OneNerd
Published on 2010-05-09T15:16:56Z Indexed on 2010/05/09 15:28 UTC
Read the original article Hit count: 132

Filed under:
|
|

Trying to grasp Prototyping in Javascript. Trying to create my own namespace to extend the String object in JavaScript.

Here is what I have so far (a snippet):

var ns {
 alert: function() {
  alert (this);
 }
}
String.prototype.$ns = ns;

As you can see, I am trying to place what will be a series of functions into the ns namespace. So I can execute a command like this:

"hello world".$ns.alert();

But the problem is, the this doesn't seem to reference the text being sent (in this case, "hello world"). What I get is an alert box with the following:

[object Object]

Not having a full grasp of the object-oriented nature of JavaScript, I am at a loss, but I am guessing I am missing something simple.

Does anyone know how to achieve this?

Thanks -

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about oop