Is it possible to overwrite a javaScript primitive data type?

The question is in itself. I know that it is possible to extend primitive data types such as string , but is it possible to overwrite it?

This is the question that was asked in the interview.

+4
source share
4 answers

No, you cannot overwrite anything. EcmaScript defines the primitive types Undefined , Null , Boolean , Number and String ; they are internal and will be used no matter what you do (for example, overwrite the global constructor of String ). Type conversion and literal evaluation are independent of any public functions, but use only these internal types and algorithms specified for them.

Of course, if someone is forcing a string with String(myval) instead of ""+myval , then assigning a global variable to String will affect this code. Any internal use still points to the "old" function.


If you talked about prototype objects for primitive types (when used as objects ), they also cannot be overwritten. You can extend these objects, but as soon as you assign, for example, Number.prototype you just lost the reference to the actual, original type of the type object. Example spec for Constructor Number :

[prototype] of the newly created object is set to the original number of the prototype of the object, one that is the initial value of Number.prototype ( 15.7.3.1 )

+2
source

Yes (edit: almost). Open the Javascript console (F12 if you use Chrome) and type

 String = function(){alert('bang!')}; 

You can rewrite (edit: almost) everything in Javascript - even in the global window context! evil.js is a library that uses this trick to rewrite as many native objects as possible.

Needless to say, this is very dangerous. I have executed the above String code, and since writing it, I have caused more than 520 Javascript errors (and I have seen "bang" several times). Internal objects are used universally, and you should not modify them if third-party code relies on them in ways you are not aware of. This is one of the reasons Prototype.js has lost its popularity, because its extension of its own objects often worked against the expectations of other code.

Edit: Actually the false statement that absolutely everything can be overwritten, as indicated in Bergi's answer . Editing done inline.

+1
source

Perhaps similar, but you should always succeed without side effects. Bad practice.

 function Array() { var obj = this; var ind = 0; var getNext = function(x) { obj[ind++] setter = getNext; if (x) alert("Data stolen from array: " + x.toString()); }; this[ind++] setter = getNext; } var a = ["private stuff"]; // alert("Data stolen from array: private stuff"); 
0
source
  • You can extend prototypes of your own types.

     String.prototype.moo = function() { console.log( 'Moo!' ) }; 'Cow says'.moo(); >> "Moo!" 
  • However, you cannot directly overwrite the built-in type constructors unless you rewrite the link to the entire object:

     String = function() { console.log( 'Custom function.' ) }; new String( 'Hello!' ); >> "Custom function." >> String {} // now you've broken your website ;) 
  • ... but still:

     'Wat?!' >> "Wat?!" // you can still create strings by typing letters in quotes 

So ... the answer is yes, but no. You can interact with your own types ( Number , Date , String ...), but you cannot redefine them entirely from scratch. They are part of the JS engine you are using (most likely this is native C ++ code), and this leads to some limitations.

0
source

All Articles