首頁 > 問答 > 主體
1
2
3
4
5
<code>http.createServer((req,res)=>{
res.write('hello world');
res.write(
'hello world'
);
console.log(typeof res);// obj
console.log(typeof res);
// obj
res.end();
res.
end
();
});</code>
如何 查看req和res的具體物件類型,這樣可以去文件中看具體詳細api.typeof列印出的是object,我希望列印出的是http.ServerResponse
怎麼搞
ringa_lee2017-07-05 10:59:14
列印函數的類別資訊:
6
7
8
9
10
11
12
13
14
15
16
17
<code>function classof(obj){
<code>
function
classof(obj){
if(typeof(obj)==="undefined")return "undefined";
if
(typeof(obj)===
"undefined"
)
return
;
if(obj===null)return "Null";
(obj===null)
"Null"
var res = Object.prototype.toString.call(obj).match(/^\[object\s(.*)\]$/)[1];
var
res = Object.prototype.toString.call(obj).match(/^\[object\s(.*)\]$/)[1];
if(res==="Object"){
(res===
"Object"
){
res = obj.constructor.name;
if(typeof(res)!='string' || res.length==0){
(typeof(res)!=
'string'
|| res.length==0){
if(obj instanceof jQuery)return "jQuery";// jQuery build stranges Objects
(obj
instanceof
jQuery)
"jQuery"
// jQuery build stranges Objects
if(obj instanceof Array)return "Array";// Array prototype is very sneaky
Array)
"Array"
// Array prototype is very sneaky
return "Object";
}
return res;
res;
// Example
console.log(classof(new Date())); // => "Date"</code>
console.log(classof(
new
Date
()));
// => "Date"</code>