JavaScript中typeof 和 instanceof 运算符的区别是什么

发布时间:2021-08-10 18:07:27 作者:Leah
来源:亿速云 阅读:181

这篇文章将为大家详细讲解有关JavaScript中typeof 和 instanceof 运算符的区别是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

1. typeof运算符

在 JS 中,基本类型有 String、Number、Boolean和 Symbol  等。此外,还有函数、对象和特殊值undefined和null。

typeof是用于确定 expression 类型的运算符:

const typeAsString = typeof expression;

expression 的计算结果是我们要查找的类型的值。expression  可以是变量myVariable,属性访问器myObject.myProp,函数调用myFunction()或数字 14。

typeof expression,取决于expression的值,结果可能为:'string', 'number', 'boolean',  'symbol', 'undefined', 'object', 'function'。

我们来看看typeof运算符每种类型的情况:

A) String:

const message = 'hello!'; typeof message; // => 'string'

B) Number:

const number = 5; typeof number; // => 'number'  typeof NaN;    // => 'number'

C) Boolean:

const ok = true; typeof ok; // => 'boolean'

D) Symbol:

const symbol = Symbol('key'); typeof symbol; // => 'symbol'

E) undefined:

const nothing = undefined; typeof nothing; // => 'undefined'

F) Objects:

const object = { name: 'Batman' }; typeof object; // => 'object'  const array = [1, 4, 5]; typeof array; // => 'object'  const regExp = /Hi/; typeof regExp; // => 'object'

G) Functions:

function greet(who) {   return `Hello, ${who}!` }  typeof greet; // => 'function'

1.1 typeof null

如上我们看到的,用 typeof 判断对象结果是 'object'。

但是,typeof null也会计算为'object'!

const missingObject = null; typeof missingObject; // => 'object'

typeof null为'object'是 JS 初始实现中的一个错误。

因此,在使用typeof检测对象时,需要另外检查null:

function isObject(object) {   return typeof object === 'object' && object !== null; }  isObject({ name: 'Batman' }); // => true isObject(15);                 // => false isObject(null);               // => false

1.2 typeof 和未定义的变量

虽然typeof expression通常决定于expression的类型,但也可以使用typeof来确定是否定义了变量。

// notDefinedVar is not defined notDefinedVar; // throws ReferenceError

typeof有一个不错的属性,当typeof评估未定义变量的类型时,不会引发 ReferenceError 错误:

// notDefinedVar is not defined typeof notDefinedVar; // => 'undefined'

变量notDefinedVar没有在当前作用域内定义。然而,typeof notDefinedVar不会抛出引用错误,而是将结果计算为  'undefined'。

我们可以使用typeof来检测是否未定义变量,如果typeof myVar === 'undefined' 为 true, 则 myVar  未定义。

2. instanceof 运算符

使用 JS 函数的通常方法是通过在其名称后添加一对括号来调用它:

function greet(who) {   return `Hello, ${who}!`; }  greet('World'); // => 'Hello, World!'

greet('World')是常规函数调用。

JS 函数可以做更多的事情:它们甚至可以构造对象!要使函数构造对象,只需在常规函数调用之前使用new关键字:

function Greeter(who) {   this.message = `Hello, ${who}!`; }  const worldGreeter = new Greeter('World'); worldGreeter.message; // => 'Hello, World!'

new Greeter('World')是创建实例worldGreeter的构造函数调用。

如何检查 JS 是否使用特定构造函数创建了特定实例?使用 instanceof 运算符:

const bool = object instanceof Constructor;

其中object是对对象求值的表达式,而Constructor是构造对象的类或函数,instanceof计算为布尔值。

worldGreeter实例是使用Greeter构造函数创建的,因此worldGreeter instanceof  Greeter计算结果为true。

从ES6 开始,可以使用 class 来定义对象。例如,定义一个类Pet,然后创建它的一个实例myPet:

class Pet {   constructor(name) {     this.name = name;   } }  const myPet = new Pet('Lily');

new Pet('Lily')是创建实例myPet的构造调用。

由于myPet是使用Pet类构造的-const myPet = new Pet('Lily'), 所以 myPet instanceof Pet 的结果为  true:

myPet instanceof Pet; // => true

但是,普通对象不是Pet的实例:

const plainPet = { name: 'Zoe' }; plainPet instanceof Pet; // => false

我们发现instanceof对于确定内置的特殊实例(如正则表达式、数组)很有用:

function isRegExp(value) {   return value instanceof RegExp; } isRegExp(/Hello/); // => true isRegExp('Hello'); // => false  function isArray(value) {   return value instanceof Array; } isArray([1, 2, 3]); // => true isArray({ prop: 'Val' }); // => false

2.1 instanceof 和父类

现在,Cat 扩展了父类Pet:

class Cat extends Pet {   constructor(name, color) {     super(name);     this.color = color;   } }  const myCat = new Cat('Callie', 'red');

不出所料,myCat是Cat类的实例:

myCat instanceof Pet; // => true

但同时,myCat也是基类Pet的一个实例:

myCat instanceof Pet; // => true

关于JavaScript中typeof 和 instanceof 运算符的区别是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. JavaScript中typeof与instanceof之间的区别是什么
  2. JavaScript中instanceof 运算符的作用是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

typeof instanceof javascript

上一篇:怎么用CSS3实现毛玻璃模糊效果

下一篇:CSS3怎么实现可爱的小黄人动画

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》