Skip to content

Instantly share code, notes, and snippets.

@munro
Created November 9, 2012 21:45
Show Gist options
  • Save munro/4048461 to your computer and use it in GitHub Desktop.
Save munro/4048461 to your computer and use it in GitHub Desktop.
Strengthen JS Types
/**
* STRENGTH!
*/
Object.prototype.toString = Array.prototype.toString = undefined;
/**
* Now operators will throw an exception when attempting
* to convert objects & arrays to a string!
*/
[] + {}; // nope
1 - []; // nope
[] * 123; // nope
'foo' >> {}; // nope
'bar' << {}; // nope
/**
* Wait, wait... why the ()? Well... JS has this useless block syntax.
* So {} is an empty block statement, which means without () JS would see:
* {}; // Cool statement!
* / {}; // Wat? You're not dividing by anything!
*/
({} / {}); // nope
[] + {} === '[object Object]'; // wat
1 - [] === 1; // wat
[] * 123 === 0; // wat
({} / {}) === '[object Object][object Object]'; // wat
'foo' >> {} === 0; // wat
'bar' >> {} === 0; // wat
isNaN({} / {}) === true; // wat...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment