3gw*_*ain 5 javascript arrays jquery object
var obj = {},ar = [],nothing=null,empty=undefined,word ='string',headorTail = true;
console.log(typeof obj) //object
console.log(typeof ar)//object
console.log(typeof nothing)//object
console.log(typeof empty)//undefined
console.log(typeof word)//string
console.log(typeof headorTail)//boolean
Run Code Online (Sandbox Code Playgroud)
但是我怎样才能获得 obj,ar,nothing as 的类型"object, array,null"- 实现这一目标的最佳方法是什么?
如果您使用 jQuery,则可以使用jQuery.type:
jQuery.type(true) === "boolean"
jQuery.type(3) === "number"
jQuery.type("test") === "string"
jQuery.type(function(){}) === "function"
jQuery.type([]) === "array"
jQuery.type(new Date()) === "date"
jQuery.type(/test/) === "regexp"
Run Code Online (Sandbox Code Playgroud)
"object"其他所有内容都以其类型返回。