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. npm升级package.json依赖包到最新版本号

    转载自:https://blog.csdn.net/syaivin/article/details/79388244?utm_source=blogxgwz1 1.安装: npm install -g ...

  2. 查看GPU占用率以及指定GPU加速程序

    GPU占用率查看: 方法一:任务管理器 如图,GPU0和GPU1的占用率如下显示. 方法二:GPU-Z软件       下面两个GPU,上面是GPU0,下面是GPU1 sensors会话框里的GPU  ...

  3. Unity 通过代码简单实现文理的灰化显示

    1.可以用于纹理的处理,也可用于模型显示的处理(比如某件准备或者服饰未获取的时候,灰化显示) 线上对比图:                     using System.Collections; ...

  4. Java基础加强总结(一)——注解(Annotation)

    一.认识注解 注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts ...

  5. Lab 3-2

    Analyze the malware found in the file Lab03-02.dll using basic dynamic analysis tools. Questions and ...

  6. Lab 1-3

    Lab 1-3 Analyze the file Lab01-03.exe. Questions and Short Answers Upload the Lab01-03.exe file to h ...

  7. 20180518VSTO多簿单表汇总

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...

  8. 【IOS学习】【Swift语言】

    基本语法: OS X playground 引入 import Cocoa IOS playground 引入 import UIKit 基本数据类型 let 定义常量 定义完成之后无法修改 var ...

  9. 一个不该写的bat

    新建bat文件,放到D盘根目录 %0|%0 进入到C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Program ...

  10. 数据库SQLserver(课本)

    一.SQL server的部署 1.数据库的基本概念 数据库通常是一个由行和列组成的二维表 数据表中的行通常叫做记录或元祖 数据表中的列通常叫做字段或属性 2.主键和外键 主键:定义主键可以保证数据的 ...