获取对象的类型信息 (JavaScript)

function classof(o) {
    if (null == o) return 'Null';
    if (undefined == o) return 'Undefined';
    return Object.prototype.toString.call(o).slice(8, -1);
}

classof([]);
classof({});
classof(Date);
classof(new Date());
classof(/./);
classof(1);
classof(1.9);
classof(null);
classof(window);

Output:

"Array"

"Object"

"Function"

"Date"

"RegExp"

"Number"

"Number"

"Null"

"Window"

posted @ 2014-08-10 10:59  PengpengSong  阅读(225)  评论(0编辑  收藏  举报