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的更多相关文章

  1. 利用Object.prototype.toString方法,实现比typeof更准确的type校验

    Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...

  2. instanceof, typeof, & Object.prototype.toString

    /** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...

  3. 判断一个变量的类型Object.prototype.toString.call

    var num = 1;alert(Object.prototype.toString.call(num)); // [object Number]var str = 'hudidit.com';al ...

  4. Object.prototype.toString.call() 区分对象类型

    判断一个对象的类型: /** * 判断对象是否为数组 * @param {Object} source 待判断的对象 * @return {Boolean} true|false */ Object. ...

  5. Object.prototype.toString.call()进行类型判断

    为什么类型判断用到Object.prototype.toString.call()进行类型判断,而不用typeof()呢? 然后翻了一下资料: Typeof 在使用 ]));/));));//[obj ...

  6. toStirng()与Object.prototype.toString.call()方法浅谈

    一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种类型转化为字符串类型的呢? 通过下面几个例子,我们便能获得答案: 1.将boolean类型的值转 ...

  7. JavaScript中toStirng()与Object.prototype.toString.call()方法浅谈

    toStirng()与Object.prototype.toString.call()方法浅谈 一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种 ...

  8. JavaScript:Object.prototype.toString方法的原理

    在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. var arr = []; console.log(Obje ...

  9. 【JavaScript】Object.prototype.toString.call()进行类型判断

    权声明:本文为博主原创文章,未经博主允许不得转载. op = Object.prototype, ostring = op.toString, ... function isFunction(it)  ...

  10. Object.prototype.toString.call() 区分对象类型(判断对象类型)

    在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种.对于数组. ...

随机推荐

  1. linux 打开一个文件现swap文件

    转自:http://blog.csdn.net/eckelwei/article/details/17078187 有时候在用vim打开文件时提示类似以下的信息: 发现交换文件 ".expo ...

  2. SqlServer中exists和in的区别

    1.in 2.exists

  3. 学习笔记23—window10 64位 python2.7 安装liblinear

    最近在使用pythin,因为要使用libsvm,所以到官网去下载libsvm.官网地址为libsvm(https://www.csie.ntu.edu.tw/~cjlin/libsvm/)结果下载下来 ...

  4. QT绘制饼图

    QT版本:QT5.6.1 QT绘制饼图,出问题的代码如下 void DrawPieDialog::paintEvent(QPaintEvent *event) { float startAngle=0 ...

  5. (转)Nandflash读写

    ------------------------------------------------------------------------------------------文章1------- ...

  6. Python自学:第二章 数字 整数

    >>>2 + 3 5 >>>3 - 2 1 >>>3 * 2 6 >>>3 / 2 1.5

  7. Lab 1-1

    LABS The purpose of the labs is to give you an opportunity to practice the skills taught in the chap ...

  8. Mac必备神器之Go2Shell

    一.作用     可以快速在当前目录打开Shell命令行窗口   二.安装 1.打开官网 http://zipzapmac.com/go2shell 2.点击下载并安装   3.点击应用图标   三. ...

  9. JS实现继承的6种方式

    使用pretotype,call实现完美继承 父类: fuction Animal(name){     this.name=name||"Animal";     this.sl ...

  10. python3—列表

    列表是什么 name = ["jim", "lilei", "lucy"] #方括号表示,逗号分隔 print(name) print(na ...