关于Object.prototype.toString.call
slice(8,-1)意思是从第8位开始(包含第8位)到最后一位之前(-1的意思就是最后一位,不包含最后一位);
Object.prototype.toString.call(boj)这个是用来判断数据类型,
如果boj是数字,得出的结果是[object Number],从零开始数,第8位就是N,最后一位的前一位就是r,所以取得Number;
如果boj是字符串,得出结果是[object String],第8位S,最后一位的前一位g,取得String
function type(boj){
return Object.prototype.toString.call(boj).slice(8,-1);
}
type(1)//"Number"
type("abc")//"String"
获取obj的类型
function getObjType(obj){
//tostring will return a constructor with a different object
var toString = Object.prototype.toString;
var map = {
'[object Boolean]' : 'boolean',
'[object Number]' : 'number',
'[object String]' : 'string',
'[object Function]' : 'function',
'[object Array]' : 'array',
'[object Date]' : 'date',
'[object RegExp]' : 'regExp',
'[object Undefined]': 'undefined',
'[object Null]' : 'null',
'[object Object]' : 'object'
};
// if obj is the instance of HTML elment
if(obj instanceof Element) {
return 'element';
}
return map[toString.call(obj)];
}
or
function getObjType(obj){
//tostring will return a constructor name with a different obj
var toString = Object.prototype.toString;
var map = {
Boolean : 'boolean',
Number : 'number',
String : 'string',
Function : 'function',
Array : 'array',
Date : 'date',
RegExp : 'regExp',
Undefined: 'undefined',
Null : 'null',
Object : 'object'
};
// if obj is the instance of HTML elment
if(obj instanceof Element) {
return 'element';
}
return map[toString.call(obj).slice(8,-1)];
}
关于Object.prototype.toString.call的更多相关文章
- 利用Object.prototype.toString方法,实现比typeof更准确的type校验
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...
- instanceof, typeof, & Object.prototype.toString
/** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...
- 判断一个变量的类型Object.prototype.toString.call
var num = 1;alert(Object.prototype.toString.call(num)); // [object Number]var str = 'hudidit.com';al ...
- Object.prototype.toString.call() 区分对象类型
判断一个对象的类型: /** * 判断对象是否为数组 * @param {Object} source 待判断的对象 * @return {Boolean} true|false */ Object. ...
- Object.prototype.toString.call()进行类型判断
为什么类型判断用到Object.prototype.toString.call()进行类型判断,而不用typeof()呢? 然后翻了一下资料: Typeof 在使用 ]));/));));//[obj ...
- toStirng()与Object.prototype.toString.call()方法浅谈
一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种类型转化为字符串类型的呢? 通过下面几个例子,我们便能获得答案: 1.将boolean类型的值转 ...
- JavaScript中toStirng()与Object.prototype.toString.call()方法浅谈
toStirng()与Object.prototype.toString.call()方法浅谈 一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种 ...
- JavaScript:Object.prototype.toString方法的原理
在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. var arr = []; console.log(Obje ...
- 【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” 五种.对于数组. ...
随机推荐
- git 修改默认编辑器
vim,notepad(windows自带),notepad++ 当然要选notepad++ 1.首先下载notepad++ 2.将notepad++安装目录放到path中 3.git config ...
- SqlServer中exists和in的区别
1.in 2.exists
- 力扣(LeetCode)13. 罗马数字转整数
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- leecode第一百二十二题(买卖股票的最佳时机II)
class Solution { public: int maxProfit(vector<int>& prices) { int len=prices.size(); ) ; , ...
- spring cloud: Hystrix(八):turbine集群监控(dashboard)
turbine是聚合服务器发送事件流数据的一个工具,hystrix的监控中,只能监控单个节点,实际生产中都为集群, 因此可以通过turbine来监控集群下hystrix的metrics情况,通过eur ...
- wpf 使用DocumentViewer打印
https://blog.csdn.net/duanzi_peng/article/details/14118937?locationNum=15 https://www.cnblogs.com/zl ...
- python 构造函数作用
1. 构造函数的作用 构造函数主要用来在创建对象时完成对对象属性的一些初始化等操作, 当创建对象时, 对象会自动调用它的构造函数.一般来说, 构造函数有以下三个方面的作用: ...
- drozer安装出现的问题
首先先检查你的手机或者模拟器是否开启了drozer agent 安装出现的问题: (1)找不到java路径: 解决方法:新建一个TXT文件,在文件中写如下内容: [executables] java ...
- 【Java】【6】JDK8 Stream操作整理
摘要: 1,List<EntityOld>转换为List<EntityNew> List<EntityOld> list = oldList; List<En ...
- vmplayer桥接以及nat配置nginx
1.环境 centos6.4 vm player nginx1.8 2.虚拟机的防火墙 参考http://blog.csdn.net/qilovehua/article/details/4550713 ...