1.使用toString()方法来检测对象类型

可以通过toString() 来获取每个对象的类型。为了每个对象都能通过 Object.prototype.toString() 来检测,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,把需要检测的对象作为第一个参数传入。

var toString = Object.prototype.toString;

toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math] //Since JavaScript 1.8.5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]

  这个用法在 tipJS 这个js 库中也有使用到.

2.Array like object

js 函数中的默认参数 arguments 就是一个 array like object.

function test(){
alert(arguments.length);
var s = [1,2,3]
alert( Array.isArray(arguments)); //false
alert( Array.isArray(s)); //true
}
test(1,2,3)

  下面来自stack-overflow上面的一个问题及回答

I'm wondering how jQuery constructs its array-like object. The key thing I'm trying to work out is how it manages to get the console to interpret it as an array and display it as such. I know it has something to do with the length property, but after playing a bit I can't quite figure it out.

I know this has no technical advantage over a normal array like object as in the example below. But I think it's an important semantic element when users are testing and debugging.

A normal Array like Object.

function foo(){
// Array like objects have a length property and it's properties use integer
// based sequential key names, e.g. 0,1,2,3,4,5,6 just like an array.
this.length = 1;
this[0] = 'hello'
}
// Just to make sure add the length property to the prototype to match the Array
// prototype
foo.prototype.length = 0; // Give the Array like object an Array method to test that it works
foo.prototype.push = Array.prototype.push // Create an Array like object
var bar = new foo; //test it
bar.push('world'); console.log(bar);
// outputs
{ 0: 'hello',
1: 'world',
length: 2,
__proto__: foo
}

  

The object has to have length and splice

> var x = {length:2, '0':'foo', '1':'bar', splice:function(){}}
> console.log(x);
['foo', 'bar']

js 中调用 Object.prototype.toString()来检测对象的类型的更多相关文章

  1. js中通过Object.prototype.toString方法----精确判断对象的类型

    判断是否为函数 function isFunction(it) {        return Object.prototype.toString.call(it) === '[object Func ...

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

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

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

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

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

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

  5. Object.prototype.toString.call()为什么可以用来检测数据类型?

    obj.toString()方法是用来干什么的 每一个对象都有一个toString()方法,默认情况下toString()被每一个Object对象继承,如果此方法未被重写,toString()返回&q ...

  6. 从toString()方法到Object.prototype.toString.call()方法

    一.toString方法和Object.prototype.toSting.call()的区别 var arr=[1,2]; 直接对一个数组调用toString()方法, console.log(ar ...

  7. 关于toString()和valueOf()以及Object.prototype.toString.call()的结合理解

    一.先说说String(): String()是全局函数,把对象的值转换为字符串. 语法:String(obj); 任何值(对象)都有String()方法,执行过程是这样的:首先,如果该对象上有toS ...

  8. Object.prototype.toString.call(arg)详解

    经常能碰到Object.prototype.toString.call对参数类型进行判断,一开始只知道怎么使用,却不了解具体实现的原理,最近恶补了一下相关知识,写个笔记加强理解,有什么不对的请指教. ...

  9. 前端面试题1:Object.prototype.toString.call() 、instanceof 以及 Array.isArray()三种方法判别数组的优劣和区别

    1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object ...

随机推荐

  1. what a malloc has to do

    1) Allocate a chunk of memory big enough to satisfy the request, and return a pointer to it.2) Remem ...

  2. iOS使用NSMutableAttributedString

    在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...

  3. px和sp什么区别

    都表示像素,只不过sp通常表示文字大小: <TextView android:layout_width="wrap_content" android:layout_heigh ...

  4. ckediter

    ckediter ##<link rel='stylesheet' href='/css/index.css' /> <script type="text/javascri ...

  5. 多项目中SVN权限管理精辟解析

    本节和大家讨论一下多项目SVN权限管理,主要包括建立版本库,修改版本库配置文件,配置允许访问的用户,设置用户访问权限.下面我们就来看一下SVN权限管理.svn权限管理svn的权限管理涉及到一下文件:p ...

  6. sqlserver 批量修改表前缀

    先把第一句话放到sqlserver查询器中执行一下.然后把查询结果复制出来,进行编辑...一看你就懂了..简单的sql语句拼装 select ' exec sp_rename "' + na ...

  7. fatal error: gst/gst.h

    ln命令使用 ln -s  源文件(src)  目标文件(dest) 进到这个文件:~/LowDA/sysroots/mx6q/usr/include$ ln -s gstreamer-0.10/gs ...

  8. 更改 android realtek的系统权限

    由于在 在删除系统的文件时候出现 Read-only file system,所以要获取权限. 推出shell adb mount mount -o rw,remount /system 就可以了

  9. wifi的UI控制。打不开UI

    部分主要是您要清楚WiFi作为什么功能,即WiFi是仅仅作为station功能,还是要开启p2p功能,当WiFi作为station功能时候开启的是wpa_supplicant服务,当WiFi作为p2p ...

  10. OpenGL红宝书例3.1 -- glBufferSubData使用

    代码实现 1.1 C++部分 GLFWwindow *window; GLuint shader_program; GLuint VAO; void init() { static const GLf ...