Skip to content

Instantly share code, notes, and snippets.

@torgeir
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torgeir/11290490 to your computer and use it in GitHub Desktop.
Save torgeir/11290490 to your computer and use it in GitHub Desktop.
js coercion on property access and method calls on primitive values
var s;
s = "wtf";
s.split = function () {};
s.split("t"); // split() works
s = String("wtf");
s.split = function () {};
s.split("t"); // split() works
s = new String("wtf");
s.split = function () {};
s.split("t"); // split() does not work
// why?
// - properties or methods of primitive values (e.g. "" or String("")) are converted to their respective objects (new String("")) before the property or method is called.
// - this is also true for other primitive types e.g. 2 and Number(2) vs. new Number(2)
// references:
// http://es5.github.io/#x11.2.1
// http://es5.github.io/#x8.7.1
// http://princepthomas.blogspot.no/2011/07/auto-boxing-javascript-primitive-types.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment