Object.prototype.toString.call()进行类型判断
为什么类型判断用到Object.prototype.toString.call()进行类型判断,而不用typeof()呢?
然后翻了一下资料:
Typeof
在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 "object"。
Object.prototype.toString.call():
- Object.prototype.toString ( )
- When the toString method is called, the following steps are taken:
- If the this value is undefined, return "[object Undefined]".
- If the this value is null, return "[object Null]".
- Let O be the result of calling ToObject passing the this value as the argument.
- Let class be the value of the [[Class]] internal property of O.
- Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".
然后举了个例子:
var oP = Object.prototype,
toString = oP.toString;
console.log(toString.call([]));//[object Array]
console.log(toString.call(''));//[object String]
console.log(toString.call({a: ''}));//[object Object]
console.log(toString.call(//));//[object RegExp]
console.log(toString.call());//[object Number]
console.log(toString.call(undefined));//[object Undefined]
console.log(toString.call(null));//[object Null]
Object.prototype.toString.call()进行类型判断的更多相关文章
- 【JavaScript】Object.prototype.toString.call()进行类型判断
权声明:本文为博主原创文章,未经博主允许不得转载. op = Object.prototype, ostring = op.toString, ... function isFunction(it) ...
- 使用Object.prototype.toString.call()方法精确判断对象的类型
在JavaScript里使用typeof判断数据类型,只能区分基本类型,即:number.string.undefined.boolean.object. 对于null.array.function. ...
- js的深入学习课程Object.prototype.toString.call()
1.通过 Object.prototype.toString.call() 进行类型判断 function isArray(obj) { return Object.prototype.toStrin ...
- Object.prototype.toString.call() 、 instanceof 以及 Array.isArray()判断数组的方法的优缺点
1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object ...
- toStirng()与Object.prototype.toString.call()方法浅谈
一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种类型转化为字符串类型的呢? 通过下面几个例子,我们便能获得答案: 1.将boolean类型的值转 ...
- JavaScript中toStirng()与Object.prototype.toString.call()方法浅谈
toStirng()与Object.prototype.toString.call()方法浅谈 一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种 ...
- Object.prototype.toString.call()方法浅谈
使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value) 1.判断基本类型: Obj ...
- Object.prototype.toString的应用
使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value)1.判断基本类型: Obje ...
- 前端面试题1:Object.prototype.toString.call() 、instanceof 以及 Array.isArray()三种方法判别数组的优劣和区别
1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object ...
随机推荐
- angular随笔
angular个别情况scope值不能改变或者不能绑定[如:指令内ctrl.$setViewValue()不能直接改变input的val值,该处需要使用scope.$apply] 如之前写的简单指令 ...
- Yocto开发笔记之《Tip-bitbake常用命令》(QQ交流群:519230208)
开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...
- IOS 学习笔记之UI
自定义控件,实现部分 - (id)initWithFrame:(CGRect)frame descriptionText:(NSArray *)inText/*需要输入两个字符串*/ { self = ...
- js011-DOM扩展
js011-DOM扩展 本章内容 理解Selecters API 使用HTML5 DOM扩展 了解转悠的DOM扩展 11.1选择符API JS中最常用的一项功能,就是根据css选择符选择与某个模式匹配 ...
- 【ZeroClipboard is not defined】的解决方法
参考:http://www.cnblogs.com/jfw10973/p/3921899.html https://github.com/zeroclipboard/zeroclipboard 近期该 ...
- CentOS_PHP_NGINX_FastCGI
yum安装nginx,它会默认作为一个服务加到系统中,启动nginx: service nginx start/nginx -s start 他有4个参数(start|stop|restart|rel ...
- ecshop 影响全局的标量lib_main.php
lib_mian.php 前台公用函数库 1.增加自定义变量 "版权所属" $copyright 或者 $smarty->assign('get_article_ ...
- Xcode文档安装
找到所需文档的下载地址,搜索.dmg 安装位置
- 学海无涯的整理Ing..........
1.文章:Understanding JavaScript Function Invocation and “this” http://yehudakatz.com/2011/08/11/unders ...
- jsp总结
JSP 定义: 1)Java Server Page, Java EE 组件,本质上是 Servlet. 2)运行在 Web Container.接收 Http Request,生成 Ht ...