javascript - 打印一个js对象的实际类型
滿天的星座
滿天的星座 2017-07-05 10:57:14
0
1
882
http.createServer((req,res)=>{
    res.write('hello world');
    console.log(typeof res);// obj
    res.end();
});

如何 查看req和res的具体对象类型,这样可以去文档中看具体详细api.typeof打印出的是object,我希望打印出的是http.ServerResponse

怎么搞

滿天的星座
滿天的星座

全部回复(1)
ringa_lee

打印函数的类信息:

function classof(obj){
    if(typeof(obj)==="undefined")return "undefined";
    if(obj===null)return "Null";
    var res = Object.prototype.toString.call(obj).match(/^\[object\s(.*)\]$/)[1];
    if(res==="Object"){
        res = obj.constructor.name;
        if(typeof(res)!='string' || res.length==0){
            if(obj instanceof jQuery)return "jQuery";// jQuery build stranges Objects
            if(obj instanceof Array)return "Array";// Array prototype is very sneaky
            return "Object";
        }
    }
    return res;
}

// Example
console.log(classof(new Date()));   // => "Date"
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!