/** * * js的类型检测方式->typeof、constuctor。 * 推荐通过构造函数来检测变量的类型。 */ var obj = {key:'value'}, arr = ["hello","javascript"], fn = function(){}, str = "hello js", num = 55, bool = true, User = function(){}, user = new User(); /*typeof测试*/ console.log(typeof obj); //obj console.log(typeof arr); //obj console.log(typeof fn); //function console.log(typeof str); //string console.log(typeof num); //number console.log(typeof bool); //boolean console.log(typeof user); //object /*constructor测试*/ console.log(obj.constructor == Object); //true console.log(arr.constructor == Array); //true console.log(str.constructor == String); //true console.log(num.constructor == Number); //true console.log(bool.constructor == Boolean);//true console.log(user.constructor == User); //true
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10060.html