类型判断----小白讲解typeof,instanceof,Object.prototype.toString.call()
typeof 1 // "number"
typeof '1' // "string"
typeof true // "boolean"
typeof {} // "object"
typeof [] // "object"
typeof function(){} // "function"
typeof undefined // "undefined"
typeof null // "object",注意!!!
let num = 1
num instanceof Number // false num = new Number(1)
num instanceof Number // true
console.log(Object.prototype.toString.call(Object) === "[object Object]");
console.log(Object.prototype.toString.call("jerry"));//[object String]
console.log(Object.prototype.toString.call(12));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
console.log(Object.prototype.toString.call(function(){}));//[object Function]
console.log(Object.prototype.toString.call([]));//[object Array]
console.log(Object.prototype.toString.call(new Date));//[object Date]
console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
function Person(){};
console.log(Object.prototype.toString.call(new Person));//[object Object]
console.log("jerry".toString());//jerry
console.log((1).toString());//1
console.log([1,2].toString());//1,2
console.log(new Date().toString());//Wed Dec 21 2016 20:35:48 GMT+0800 (中国标准时间)
console.log(function(){}.toString());//function (){}
console.log(null.toString());//报错
console.log(undefined.toString());//报错
var arr=[1,2,3];
console.log(Array.prototype.hasOwnProperty("toString"));//true
console.log(arr.toString());//1,2,3
delete Array.prototype.toString;//delete操作符可以删除实例属性
console.log(Array.prototype.hasOwnProperty("toString"));//false
console.log(arr.toString());//"[object Array]"
参考:
https://www.cnblogs.com/youhong/p/6209054.html
https://www.cnblogs.com/Yance/p/7655320.html
类型判断----小白讲解typeof,instanceof,Object.prototype.toString.call()的更多相关文章
- typeof()与Object.prototype.toString.call()
用typeof方法只能初步判断number string undefined boolean object function symbol这几种初步类型 使用Object.prototype.toSt ...
- typeof 、Object.prototype.toString和 instanceof
数据类型 js 基本类型包括:Undefined symbol null string boolean number js 引用类型包括:object array Date RegExp typeo ...
- typeof 和 Object.prototype.toString.call 数据类型判断的区别
使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种. 但 Object.prototype ...
- js的深入学习课程Object.prototype.toString.call()
1.通过 Object.prototype.toString.call() 进行类型判断 function isArray(obj) { return Object.prototype.toStrin ...
- JS基础-数据类型判断typeof、instanceof、Object.prototype.toString
typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.proto ...
- 使用Object.prototype.toString.call()方法精确判断对象的类型
在JavaScript里使用typeof判断数据类型,只能区分基本类型,即:number.string.undefined.boolean.object. 对于null.array.function. ...
- 【Object.prototype.toString.call()】---判断某个对象属于哪种内置类型------【巷子】
预热:目前我们知道区分对象类型的方式有三种 :type instanceof Object.prototype.toString.call. 那么这三种的区别是什么呢? type 在 Java ...
- 前端面试题1:Object.prototype.toString.call() 、instanceof 以及 Array.isArray()三种方法判别数组的优劣和区别
1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object ...
- toStirng()与Object.prototype.toString.call()方法浅谈
一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种类型转化为字符串类型的呢? 通过下面几个例子,我们便能获得答案: 1.将boolean类型的值转 ...
随机推荐
- IIS301重定向:将不带www的域名跳转到带www上
首先你的域名有这两条解析记录 进入服务器IIS,添加2个站点,如下图 第一个正常绑定你的域名:www.baidu.com 第二个绑定不带www的域名:baidu.com 然后点开ncgd-no-www ...
- EasyUI Datagrid的简单使用
此前同样写过EasyUI Datagrid的demo,好记性不如烂笔头,何况记性也不是那么好,赶紧记录一下.照搬上一篇EasyUI Tree的格式. 实现效果:获取数据库表的数据,在EasyUI Da ...
- noip模拟赛 花
[问题描述]商店里出售n种不同品种的花.为了装饰桌面,你打算买m支花回家.你觉得放两支一样的花很难看,因此每种品种的花最多买1支.求总共有几种不同的买花的方案?答案可能很大,输出答案mod p的值. ...
- hdu 3804树链剖分+离线操作
/* 树链刨分+离线操作 题意:给你一棵树,和询问x,y 从节点x--节点1的小于等于y的最大值. 解:先建一个空树,将树的边权值从小到大排序,将询问y按从小到大排序 对于每次询问y将小于等于y的边权 ...
- Hihocoder 1333 (splay)
Problem 平衡树 splay2 题目大意 维护一个序列,支持四种操作: 操作1:添加一个数,编号为x,权值为y. 操作2:删除编号在区间[x,y]内的数. 操作3:将编号在区间[x,y]内的数的 ...
- JavaMelody开源系统性能监控
https://blog.csdn.net/itopme/article/details/8618067
- UnicodeEncodeError: 'gbk' codec can't encode character '\u2022' in position
在GBK解码时忽略掉不能解码的数据 self.file.write(content.encode("gbk", 'ignore').decode("gbk", ...
- ubuntu 16.04网卡找不到eth0
自15版本开始就不叫eth0.可以通过ifconfig进行查看: ifconfig -a 其中enp3s0才是网卡的名称,lo为环路. 参考: http://blog.csdn.net/christn ...
- 程序员之---C语言细节20(符号和有符号之间转换、两数相加溢出后数值计算)
主要内容:无符号和有符号之间转换.两数相加溢出后数值计算 #include <stdio.h> /* 这个函数存在潜在漏洞 */ float sum_elements(float a[], ...
- 1.3-动态路由协议EIGRP
EIGRP(Enhanced IGRP) EIGRP的特点: IGRP/EIGRP都是CISCO的私有协议. 1:是唯一的一种LS/DV的混合协议. 2:Rapid convergence EIGRP ...