为了巩固基础,我会通过实例来详细说明,让我们一起搞懂 typeof 和 instanceof。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
console.log( 'typeof 123', ':', typeof 123 );
// number
console.log( "typeof 'str'", ':', typeof 'str' );
// string
console.log( "typeof !'0'", ':', typeof !'0' );
// boolean
console.log( "typeof new Function()", ':', typeof new Function() );
// function
console.log( 'typeof myname', ':', typeof myname );
// undefined
console.log( 'typeof null', ':', typeof null );
// object
console.log( "typeof {name: 'hello'}", ':', typeof {name: 'hello'} );
// object
console.log( "typeof [1,2,3]", ':', typeof [1,2,3] );
// object
console.log( "[1,2] instanceof Array", ':', [1,2] instanceof Array );
// true
console.log( "Array.isArray([1,2])", ':', Array.isArray([1,2]) );
// true
console.log( "({name: 'he'}) instanceof Object", ':', ({name: 'he'}) instanceof Object );
// true
console.log( "new Date() instanceof Date", ':', new Date() instanceof Date );
function Person(){}
console.log( new Person() instanceof Person );
// true
// 接下來是繼承
function Parent(){};
function Child(){};
function Other(){};
Child.prototype = new Parent();
let child = new Child();
console.log( child instanceof Child ); // true
console.log( child instanceof Parent ); // true
console.log( child instanceof Object ); // true
console.log( child instanceof Other ); // false
console.log( Parent.prototype.__proto__ === Object.prototype ); // true
// instanceof 只能用来判断两个对象是否属于实例关系, 而不能判断一个对象实例具体属于哪种类型。
function fun() {}
console.log(typeof fun); // function
console.log(fun instanceof Function); // true
console.log(fun instanceof Object); // true </script>
</body>
</html>

基础文之-----typeof 和 instanceof的更多相关文章

  1. JS基础-数据类型判断typeof、instanceof、Object.prototype.toString

    typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.proto ...

  2. 深入了解typeof与instanceof的使用场景及注意事项

    JavaScript中的数据类型分为两类,undefined,number,boolean,string,symbol,bigint,null[1]组成的基础类型和Object.Function.Ar ...

  3. JS中typeof与instanceof的区别

    JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: Typeof typeof 是一个一元运算,放在一个运算数之前 ...

  4. js中typeof和instanceof

    对于typeof和instanceof,我们经常用来检测数据的类型.typeof可以检测Number.Boolean.String.Undefined类型,对于其他类型的数据都返回为object:而i ...

  5. JS typeof与instanceof的区别

    typeof 与 instanceof 通常是用来判断一个变量的类型,二者有如下区别: typeof: 判断一个变量的类型,返回值是字符串形式,返回结果有如下几种: number,boolean,st ...

  6. JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

  7. 原生JS:delete、in、typeof、instanceof、void详解

    delete.in.typeof.instanceof.void详解 本文参考MDN做的详细整理,方便大家参考[MDN](https://developer.mozilla.org/zh-CN/doc ...

  8. typeof和instanceof

    JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: typeof typeof 是一个一元运算,放在一个运算数之前 ...

  9. 面向对象的程序设计(二)理解各种方法和属性typeof、instanceof、constructor、prototype、__proto__、isPrototypeOf、hasOwnProperty

    //理解各种方法和属性typeof.instanceof.constructor.prototype.__proto__.isPrototypeOf.hasOwnProperty. //1.typeo ...

随机推荐

  1. 二十二、Pod存储之volume

    Pod 的存储之volume ​容器磁盘上的文件的生命周期是短暂的,这就使得在容器中运行重要应用时会出现一些问题.首先,当容器崩溃时,kubelet 会重启它,但是容器中的文件将丢失--容器以干净的状 ...

  2. 驱动开发:内核LDE64引擎计算汇编长度

    本章开始LyShark将介绍如何在内核中实现InlineHook挂钩这门技术,内核挂钩的第一步需要实现一个动态计算汇编指令长度的功能,该功能可以使用LDE64这个反汇编引擎,该引擎小巧简单可以直接在驱 ...

  3. mybatis-核心配置文件讲解

    核心配置文件详解 核心配置文件中的标签必须按照固定的顺序(有的标签可以不写,但顺序一定不能乱): properties.settings.typeAliases.typeHandlers.object ...

  4. Seata 1.5.2 源码学习

    文章有点长,我决定用半个小时来给您分享~ 基于Seata 1.5.2,项目中用 seata-spring-boot-starter 1. SeataDataSourceAutoConfiguratio ...

  5. 【Azure API 管理】Azure APIM服务集成在内部虚拟网络后,在内部环境中打开APIM门户使用APIs中的TEST功能失败

    问题描述 使用微软API管理服务(Azure API Management),简称APIM. 因为公司策略要求只能内部网络访问,所以启用了VNET集成.集成方式见: (在内部模式下使用 Azure A ...

  6. 解决Halcon转C#时,图像显示的问题

    不知道大家在使用Halcon进行图像处理,由于要连续处理多张图片,转为C#代码的时候,使用了Halcon控件显示图像,但是运行的时候,中间的其他图片没有显示在控件上,之前我一直以为是运行速度快导致看不 ...

  7. 2022春每日一题:Day 41

    题目:I Hate It 一个基础的线段树模板,单点修改+区间查询 代码: #include <cstdio> #include <cstdlib> #include < ...

  8. MediatRPC - 基于MediatR和Quic通讯实现的RPC框架,比GRPC更简洁更低耦合,开源发布第一版

    大家好,我是失业在家,正在找工作的博主Jerry.作为一个.Net架构师,就要研究编程艺术,例如SOLID原则和各种设计模式.根据这些原则和实践,实现了一个更简洁更低耦合的RPC(Remote Pro ...

  9. 谷歌、微软、Meta?谁才是 Python 最大的金主?

    你知道维护 Python 这个大规模的开源项目,每年需要多少资金吗? 答案是:约 200 万美元! PSF(Python 软件基金会)在 2022 年 6 月发布了 2021 的年度报告,其中披露了以 ...

  10. 关于cannot remove ‘directory': Directory not empty的解决办法

    解决方法 首先你应该使用 rm -rf 目录名 这样确保可以递归删除目录 如果出现 cannot remove 'directory': Directory not empty 报错信息,重启电脑解决 ...