Heim  >  Fragen und Antworten  >  Hauptteil

javascript – Gibt den tatsächlichen Typ eines JS-Objekts aus

http.createServer((req,res)=>{
    res.write('hello world');
    console.log(typeof res);// obj
    res.end();
});

So überprüfen Sie die spezifischen Objekttypen von req und res, damit Sie zum Dokument gehen können, um die spezifischen Details anzuzeigen. api.typeof druckt das Objekt aus, und ich hoffe, dass es sich bei dem ausgedruckten Objekt um http.ServerResponse handelt

Wie es geht

滿天的星座滿天的星座2663 Tage vor899

Antworte allen(1)Ich werde antworten

  • ringa_lee

    ringa_lee2017-07-05 10:59:14

    打印函数的类信息:

    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"

    Antwort
    0
  • StornierenAntwort