最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is it possible to overwrite javaScript primitive data type? - Stack Overflow

programmeradmin0浏览0评论

Question is self explanatory. I know it is possible to extend primitive data types such as string but is it possible to overwrite it?

This is a question that has been asked in an interview.

Question is self explanatory. I know it is possible to extend primitive data types such as string but is it possible to overwrite it?

This is a question that has been asked in an interview.

Share Improve this question asked Feb 28, 2013 at 12:57 JS-coderJS-coder 3,2812 gold badges17 silver badges14 bronze badges 6
  • 3 Overwrite what exactly? – Bergi Commented Feb 28, 2013 at 13:00
  • eg. String, lets say string should always have one number. – JS-coder Commented Feb 28, 2013 at 13:02
  • It used to be possible to overwrite the Array constructor for instance, but has been fixed because of JSON-linked security holes, see there : ejohn/blog/re-securing-json - says it's not possible to overwrite global objects' constructors anymore, at least in FF (suppose it's the case in all major browsers by now). – darma Commented Feb 28, 2013 at 13:04
  • What do you mean by "have one number"? That "somestring" + 5 === 6? – Bergi Commented Feb 28, 2013 at 13:04
  • 2 Primitive datatypes are not extensible at all. If you mean extending String.prototype for example, then you are extending String objects, not primitive strings. That said, you can write your own JavaScript engine and do whatever you want! – Felix Kling Commented Feb 28, 2013 at 13:18
 |  Show 1 more ment

4 Answers 4

Reset to default 2

No, you cannot overwrite anything. EcmaScript defines the primitive types Undefined, Null, Boolean, Number, and String; these are internal and will be used regardless of what you are doing (for example overwriting the global String constructor). Type conversion and evaluation of literals does not rely on any public functions but uses only these internal types and the algorithms specified for them.

Of course, if someone does string coercion with String(myval) instead of ""+myval assigning to the global String variable will have an effect on that code. Any internal use would still point to the "old" function.


If you were talking about prototype objects for the primitive types (when used as objects), those are not overwritable as well. You may extend those objects, but as soon as you assign to e.g. Number.prototype you just have lost a reference to the actual, original number protype object. Example spec for The Number constructor:

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

Yes (edit: almost). Open up a Javascript console (F12 if you're using Chrome) and type

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

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

Needless to say, this is extremely dangerous. I performed the String remapping code above, and since writing it down I've caused over 520 Javascript errors (and I've seen 'bang' alerted quite a few times). Native objects are used everywhere, and you shouldn't modify these in case 3rd party code relies on them in ways you don't know about. This is one of the reasons Prototype.js lost popularity — because its extension of native objects would often work against the expectations of other code.

Edit: Factually incorrect assertion that absolutely everything could be overwritten, as pointed out in Bergi's answer. Edits made inline.

  1. You can extend prototypes of native types.

    String.prototype.moo = function() { 
       console.log( 'Moo!' ) 
    };
    'Cow says'.moo();
    >> "Moo!"
    
  2. However you cannot directly overwrite constructors of built-in types unless you overwrite the reference to the entire object:

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

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

So... the answer is "yes but no". You can mess with native types (Number, Date, String...) but you cannot re-define them entirely from scratch. They're a part of JS engine that you're using (most likely native C++ code) and this brings some limitations.

Possible like this but you must always succeed without side effects.Not good 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)

  1. 暂无评论