JavaScript prototype question

Trying to understand prototyping in Javascript. Trying to create your own namespace to extend a String object in JavaScript.

Here is what I still have (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 in the ns namespace. Therefore, I can execute the command as follows:

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

But the problem is that this one does not refer to the text being sent (in this case, โ€œhello worldโ€). I get a warning window with the following:

[object of object]

Not having a complete understanding of the object-oriented nature of JavaScript, I am at a loss, but I assume that I am missing something simple.

- , ( )? , (ns.alert( "hello world" );), .

-

+3
1

, , , this ( ).

, "hello world".$ns.alert(); this alert, "hello world".$ns, String.prototype.$ns.

, ( ) prototype , , .

+5

Source: https://habr.com/ru/post/1744682/


All Articles