基础文之-----typeof 和 instanceof
为了巩固基础,我会通过实例来详细说明,让我们一起搞懂 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的更多相关文章
- JS基础-数据类型判断typeof、instanceof、Object.prototype.toString
typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.proto ...
- 深入了解typeof与instanceof的使用场景及注意事项
JavaScript中的数据类型分为两类,undefined,number,boolean,string,symbol,bigint,null[1]组成的基础类型和Object.Function.Ar ...
- JS中typeof与instanceof的区别
JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: Typeof typeof 是一个一元运算,放在一个运算数之前 ...
- js中typeof和instanceof
对于typeof和instanceof,我们经常用来检测数据的类型.typeof可以检测Number.Boolean.String.Undefined类型,对于其他类型的数据都返回为object:而i ...
- JS typeof与instanceof的区别
typeof 与 instanceof 通常是用来判断一个变量的类型,二者有如下区别: typeof: 判断一个变量的类型,返回值是字符串形式,返回结果有如下几种: number,boolean,st ...
- JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式
相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...
- 原生JS:delete、in、typeof、instanceof、void详解
delete.in.typeof.instanceof.void详解 本文参考MDN做的详细整理,方便大家参考[MDN](https://developer.mozilla.org/zh-CN/doc ...
- typeof和instanceof
JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: typeof typeof 是一个一元运算,放在一个运算数之前 ...
- 面向对象的程序设计(二)理解各种方法和属性typeof、instanceof、constructor、prototype、__proto__、isPrototypeOf、hasOwnProperty
//理解各种方法和属性typeof.instanceof.constructor.prototype.__proto__.isPrototypeOf.hasOwnProperty. //1.typeo ...
随机推荐
- 微信小程序之顶部导航栏
wxml: <!--导航条--><view class="navbar"> <text wx:for="{{navbar}}" d ...
- while循环条件不成立却无法跳出死循环的问题
在进入循环的时候,实际上是将A从内存加载到寄存器里面运行的,在整个循环中,A这个变量都只是在读取寄存器里面的值. 而当进入中断的时候,中断里面会从内存加载A到寄存器,修改完之后又存到内存里,然后退出中 ...
- excel公式与快捷操作
将首行的公式,运用到这一整列 1.选中要输入公式的第一个单元格,SHIFT+CTRL+方向键下,在编辑栏中输入公式,按下CTRL+回车: 2.先输入要填充的公式,按下SHIFT+CTRL+方向键下,再 ...
- java判断手机号三大运营商归属的工具类
package com.tymk.front.third; import java.util.regex.Pattern; public class OperatorsUtil { /** * 中国电 ...
- K8S之prometheus-operator监控
prometheus-operator 1. Prometheus Operator介绍 介绍文章:http://t.zoukankan.com/twobrother-p-11164391.html ...
- 查看服务器出口ip
[root@iZap201hv2fcgry1alvbznZ ~]# curl cip.cc IP : xxx.xxx.xx.xx 地址 : 中国 浙江 绍兴 运营商 : 移动 数据二 : 浙江省绍兴市 ...
- centos7 redis6.2.6安装
1. 源码包下载并解压 wget http://download.redis.io/releases/redis-6.2.6.tar.gz tar -zxvf redis-6.2.6.tar.gz 2 ...
- <二>自己实现简单的string
我们结合运算符重载知识实现string 类 在自己实现的String类中可以参考C++中string的方法 例如构造,加法,大小比较,长度,[] 等操作. 当前的MyString 类中,暂时不加入迭代 ...
- 【Java EE】Day11 BootStrap、响应式布局、栅格系统、CSS样式、案例
一.BootStrap介绍 https://v3.bootcss.com/css/#overview 1.概念 基于三剑客开发的前端开发框架 定义了许多css样式和js插件,从而得到丰富的页面效果 依 ...
- 你的项目使用Optional了吗?
1.基本概念 java.util.Optional<T>类本质上就是一个容器,该容器的数值可以是空代表一个值不存在,也可以是非空代表一个值存在. 2.获取对象 2.1 相关方法 2.2 案 ...