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 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 (get the source text from the nested object)? Short of that, I am left with having to do procedural programming ( ns.alert("hello world"); ) which I am trying to avoid.
Thanks -