2020-12-18

判断数据类型的方法

1:typeof:基本数据类型没有问题,引用数据类型有问题。
2: instanceof:基本数据类型会有问题,而引用数据类型没有问题。

//基本数据类型
console.log("1" instanceof String); //false
console.log(1 instanceof Number);  //false
console.log(true instanceof Boolean);  //false
//引用数据类型
console.log([] instanceof Array);  //true
console.log(function(){} instanceof Function);  //true
console.log({} instanceof Object);  //true

3:constructor:除了undefined和null,其它变量都能使用constructor判断类型。

console.log(("1").constructor === String);  //true
console.log((1).constructor === Number);  //true
console.log((true).constructor === Boolean);  //true
console.log(([]).constructor === Array);  //true
console.log((function(){}).constructor === Function);  //true
console.log(({}).constructor === Object);  //true
console.log((null).constructor === Null);   //报错
console.log((undefined).constructor === Undefined);  //报错

4:Object.prototype.toString.call();

console.log(Object.prototype.toString.call(1));  //[object Number]
console.log(Object.prototype.toString.call("1"));  //[object String]
console.log(Object.prototype.toString.call(true));  //[object Boolean]
console.log(Object.prototype.toString.call([]));  //[object Array]
console.log(Object.prototype.toString.call(function(){})); //[object Function]
console.log(Object.prototype.toString.call({}));  //[object Object]
console.log(Object.prototype.toString.call(null));  //[object Null]
console.log(Object.prototype.toString.call(undefined));  //[object Undefined]

js预解析

JS运行时会将内部先把带有var和function关键字的声明提升到当前作用域的最顶端,
var 定义的变量预设为undefined,function设为函数字符串
函数的预解析优先级要比用 var 声明的变量优先级高
函数内部也会进行预解析

	alert(a);//输出函数a
		var a= 100;
		function a () {
			console.log("hello")
		}
//函数内部的预解析
function fn(){
			console.log(a);//输出函数a
			var a = 1;
			function a(){
				console.log(1)
			}
		}
		fn()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值