3

String is immutable which means we can only read its property, not modify or create or delete any of the property or method. So basically string is freeze.

My question is how is it possible to add a new method to the string prototype?

var a = '';
String.prototype.hi = function(){
console.log('hi'); 
}
a.hi()
Output: hi

Why is it not throwing any error?

Kristianmitk
  • 4,528
  • 5
  • 26
  • 46
Swetha Lakshmi
  • 259
  • 5
  • 12
  • 1
    But why you wanna do that ? why can't you use a simple function and pass string as argument, adding `functions` to `prototype` is not a good practice – Code Maniac Aug 06 '19 at 11:03
  • 1
    functions are added to prototypes all the time @CodeManiac – Liam Aug 06 '19 at 11:03
  • 1
    @Liam It is still not good practice (unless it is done for polyfills). See https://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice – str Aug 06 '19 at 11:04
  • 3
    When you say *String is immutable*, it actually means instances of String class are immutable. But you can still add static functions to `String` class – Rajesh Aug 06 '19 at 11:04
  • @CodeManiac Yeah, you're right but I'm giving example for my doubt for better understanding. – Swetha Lakshmi Sep 10 '19 at 07:40

4 Answers4

1

String is immutable which means we can only read its property, not modify or create or delete any of the property or method

This is not true. By strings being immutable we mean that the string object itself is freezed. This doesn't mean that String.prototype, which is a separate object, is freezed. When we add properties to the String.prototype object, that doesn't mean we've mutated any strings. It's just another object not the string itself.

frogatto
  • 28,539
  • 11
  • 83
  • 129
0

String in Javascript is immutable, it is a true statement. So, you are correct. So, the value of the string is immutable but not it's prototype. String inherited the prototype from its parent object. The functions available in the prototype do not modify the value of string, it returns a new instance of string.

When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.

The value of string in javascript is loosely coupled with prototype. If you replace the prototype of string with empty object, there will be no change of the value. You will get the exactly same value for the string when you will access it.

PaulShovan
  • 2,140
  • 1
  • 13
  • 22
0

About prototype, you've got already enough answers what is it...I gues that you even knew it.

So in your example you are not mutating the string it self, what you've noticed is called primitive wrapper objects

MDN

Example:

var a = 'A';
String.prototype.test = function() {
  return this;
}


console.log('a primitive type =>', typeof a)

console.log('a primitive object wrapper type =>', typeof a.test());
idmitrov
  • 464
  • 1
  • 5
  • 13
-1

Yes, It is possible to add new functions to String prototype as like for other objects.

Though it is worth looking at this information present on JavaScript|MDN String Just remove invocation from your code.

Code:

var a = '';
String.prototype.hi = function(){
console.log('hi'); 
}
a.hi();
Swapnil Shukla
  • 229
  • 1
  • 8
  • Yeah, it's possible but why and how?. Because, String is immutable which is not like any other object – Swetha Lakshmi Aug 06 '19 at 11:34
  • All of the String methods return a new string rather than modifying the actual one. One can simply not modify its value but the prototypes. Also, Strings are not the only thing immutable in javascript. – Swapnil Shukla Aug 06 '19 at 11:44