javascript变量类型的确定

1.typeof操作符来确认类型

1)typeof操作符一共会产生undefined、string、number、boolean、object、function、symbol、bigint8种结果,不严谨地说,跟javascript的8(7 + 1)种数据类型的数量是一样的,但是数据类型是没有Function这种类型的,typeof也没有null这种结果。

2)typeof一个function(他是引用类型)的结果是function,typeof null的结果是object

function fn() {
}
console.log(typeof fn); //function
console.log(typeof null); //object

3)typeof一个class的结果也是function

class myClass {
}
console.log(typeof myClass) //function

3)typeof一个Array的结果是object(所以用typeof是分辨不出来具体的引用类型的)

const arr1 = [];
const obj1 = {};
console.log(typeof arr1) //object
console.log(typeof obj1) //object

4)通过原始字面量方式创建的原始值和通过构造函数创建的引用值的typeof结果

const str1 = 'abc';
const str2 = new String('def');
console.log(typeof str1)  //string;
console.log(typeof str2); //object

const bool1 = true;
const bool2 = new Boolean(false);
// 下面这个叫转型函数
const bool3 = Boolean(false);
console.log(typeof bool1);
console.log(typeof bool2);
console.log(typeof bool3);

2.instanceof帮助确定对象的具体类型(通过原型链来确认)

1)基本的

const str1 = 'arthur';
const str2 = new String('hsing');
console.log(str1 instanceof String);  //false
console.log(str2 instanceof String);  //true

const obj1 = {};
console.log(obj1 instanceof Object);  //true

const arr1 = [];
console.log(arr1 instanceof Array);   //true
console.log(arr1 instanceof Object);  //true

const fn1 = function () {
}
console.log(fn1 instanceof Function); //true
console.log(fn1 instanceof Object); //true

2)自己的写的构造函数

function Parent(name) {
  this.name = name;
}
function Child(age) {
  this.age = age;
}
const p = new Parent();
const c = new Child();
console.log(p instanceof Parent); //true
console.log(c instanceof Parent);//false

3)继承

function Parent(name) {
  this.name = name;
}
function Child(age) {
  this.age = age;
}
//继承
Child.prototype = Object.create(Parent.prototype);
const p = new Parent();
const c = new Child();
console.log(p instanceof Parent); //true
console.log(c instanceof Parent);//true

Child.prototype = Object.create(Parent.prototype);在这一句代码执行后,从此通过Child构造函数实例化出来的对象c的原型链上就会有Parent对象的原型,javascript引擎会认为c也是Parent的实例。

为了证明这个说法,再贴下如下代码:

function Child() {
}
Child.prototype = Object.create(Array.prototype);
console.log(new Child() instanceof Array);  //true

在上面代码中,所有通过构造函数Child实例化出来的对象的原型链上都有Array的原型,故最后一行的instanceof判断为true,如果不懂Object.create的机制的小伙伴,可以看下面的代码,下面的代码和上面的代码实现的效果是一样的:

function Child() {
}
Child.prototype = [];
console.log(new Child() instanceof Array);  //true

为了排除实例的影响,我们直接更换原型

function Child() {
}
function Parent() {
}
Child.prototype = Parent.prototype;
console.log(new Child() instanceof Parent); //true
console.log(new Child() instanceof Child);  //true

3.最后总结一下typeof和instanceof:

typeof对原始值很友好,可以区分出不同的原始值,但是对于引用值,除了function以外,其余的typeof结果全是object,那么在有些场景下,比如说想区分一个数组和一个对象(字面量对象之类的),那么用typeof是无法进行正确的区分的,这时instanceof就可以起到作用了。**

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值