js基础 判断数据类型

数据类型检测

  1. typeof

    typeof 是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型。返回的结果用该类型的字符串(全小写字母)形式表示,包括以下 7 种:number、boolean、symbol、string、object、undefined、function 等。

 typeof'';// string 有效
typeof1;// number 有效
typeofSymbol();// symbol 有效
typeoftrue;//boolean 有效
typeofundefined;//undefined 有效
typeofnull;//object 无效
typeof[] ;//object 无效
typeofnewFunction();// function 有效
typeofnewDate();//object 无效
typeofnewRegExp();//object 无效
  1. instanceof

    instanceof 是用来判断 A 是否为 B 的实例,表达式为:A instanceof B,如果 A 是 B 的实例,则返回 true,否则返回 false。 在这里需要特别注意的是:instanceof 检测的是原型,instanceof 只能用来判断两个对象是否属于实例关系, 而不能判断一个对象实例具体属于哪种类型

  2. constructor 检测构造函数

    利用原型对象上的 constructor 引用了自身,当 F 作为构造函数来创建对象时,原型上的 constructor 就被遗传到了新创建的对象上, 从原型链角度讲,构造函数 F 就是新对象的类型。这样做的意义是,让新对象在诞生以后,就具有可追溯的数据类型

    1.null 和 undefined 是无效的对象,因此是不会有 constructor 存在的,这两种类型的数据需要通过其他方式来判断。
    2.函数的 constructor 是不稳定的,这个主要体现在自定义对象上,当开发者重写 prototype 后,原有的 constructor 引用会丢失,constructor 会默认为 Object
    3.为什么变成了 Object?
    因为 prototype 被重新赋值的是一个 { }, { } 是 new Object() 的字面量,因此 new Object() 会将 Object 原型上的 constructor 传递给 { },也就是 Object 本身。
    因此,为了规范开发,在重写对象原型时一般都需要重新给 constructor 赋值,以保证对象实例的类型不被篡改。

   	console.log("".constructor===String) // true
   	console.log(new Number(1).constructor===Number) // true
   	console.log(true.constructor===boolean) // true
   	console.log(new Function().constructor===Function) // true
   	console.log(new Date().constructor===Date) // true
   	console.log(new Error().constructor===Error) // true
   	console.log([].constructor===Array) // true
   	console.log(document.constructor===HTMLDocument) // true
   	console.log(window.constructor===window) // true
  1. Object.prototype.toString.call 检测数据类型 (推荐)
    toString() 是 Object 的原型方法,调用该方法,默认返回当前对象的 [[Class]] 。这是一个内部属性,其格式为 [object Xxx] ,其中 Xxx 就是对象的类型。
    对于 Object 对象,直接调用 toString() 就能返回 [object Object] 。而对于其他对象,则需要通过 call / apply 来调用才能返回正确的类型信息。
 		Object.prototype.toString.call('') ;  // [object String]
		Object.prototype.toString.call(1) ;   // [object Number]
		Object.prototype.toString.call(true) ;// [object Boolean]
		Object.prototype.toString.call(Symbol());//[object Symbol]
		Object.prototype.toString.call(undefined) ;// [object Undefined]
		Object.prototype.toString.call(null) ;// [object Null]
		Object.prototype.toString.call(newFunction()) ;// [object Function]
		Object.prototype.toString.call(newDate()) ;// [object Date]
		Object.prototype.toString.call([]) ;// [object Array]
		Object.prototype.toString.call(newRegExp()) ;// [object RegExp]
		Object.prototype.toString.call(newError()) ;// [object Error]
		Object.prototype.toString.call(document) ;// [object HTMLDocument]
		Object.prototype.toString.call(window) ;//[object global] window 是全局对象 global 的引用
	typeof [ value ] 返回当前值得数据类型  “数据类型”
	返回结果都是字符串
	局限性:typeof null => "object"
				不可以细分对象类型,数组对象和普通对象都为 “object” 
	typeof 默认认为前几位为0的为object ,null 存贮为000 ;
	typeof 12 //"number"
	typeof null // "object"  // 所有的值在内存中都是以二进制存贮的 
	typeof "number" // "string" 
	typeof undefined // "undefined"
	typeof true // "boolean" 
	typeof Symbol('1'); // "symbol"
	typeof BigInt('1') // "bigint"
	typeof {} // "object"
	typeof [] //"object"
	typeof function(){} // "function" 
	// 例子
	let a = typeof typeof typeof [12,14]
	console.log(a) // "string"

	let b = parseFloat("left:'100px'");
	if(b===100){
		console.log(100);
	}else if(b===NaN){
		console.log(nan)
	}else if(typeof b === "number"){
		console.log("number")
	}else {
		console.log("Invalid number")
	}// number
	
	// parseInt 处理的值是字符串,从字符串的左侧开始查找有效数字字符,
	// 遇到非有效字符停止查找,如果处理的值不是字符串,需要先转化为字符串,在进行查找
	// Number 直接用浏览器最底层的数据类型测试机制来完成 

	parseInt("") // NaN;
	Number("") // 0;
	isNaN("") // false   false 代表有效数字
	parseInt(null) ; // parseInt("null") NaN 
	Number("21px")  // NaN
	parseInt('1.2px') + parseFloat('1px') + typeof 	parseInt(null) // 1 + 1 + typeof NaN => 2+"number" =>"2number"
	typeof !parseInt(null) + !isNaN(null)  // "booleantrue"
	10 + undefined  // NaN 
	NaN+[] //NaN
	"123dadsfa" + {}  // "123dadsfa[object,'object]"
	//对象转换字符串先调用valueof 在调用toString
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值