正好看到Object.prototype.toString(),通過解釋發(fā)現(xiàn)不光是將現(xiàn)有對(duì)象內(nèi)容轉(zhuǎn)為字符串。
If this method is not overridden in a custom object, toString() returns "[object type]", where type is the object type.
大致理解是在沒有被自定義默認(rèn)的情況下,比如僅僅是通過new構(gòu)造,會(huì)返回[object type]
var o = new Object();
o.toString(); // returns [object Object]
于是利用這個(gè)性質(zhì),被玩慘了:
var toString = Object.prototype.toString;
toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math]
// Since JavaScript 1.8.5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]