获取对象的类型信息 (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"

原文地址:https://www.cnblogs.com/pengpenghappy/p/3902523.html