Rumah  >  Soal Jawab  >  teks badan

javascript - Cetak jenis sebenar objek js

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

Bagaimana untuk menyemak jenis objek tertentu req dan res, supaya anda boleh pergi ke dokumen untuk melihat butiran khusus api.typeof mencetak objek, dan saya berharap apa yang dicetak ialah http.ServerResponse

.

Bagaimana caranya

滿天的星座滿天的星座2663 hari yang lalu902

membalas semua(1)saya akan balas

  • ringa_lee

    ringa_lee2017-07-05 10:59:14

    Cetak maklumat kelas fungsi:

    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"

    balas
    0
  • Batalbalas