js 中调用 Object.prototype.toString()来检测对象的类型
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()来检测对象的类型的更多相关文章
- js中通过Object.prototype.toString方法----精确判断对象的类型
		
判断是否为函数 function isFunction(it) { return Object.prototype.toString.call(it) === '[object Func ...
 - Object.prototype.toString.call() 区分对象类型
		
判断一个对象的类型: /** * 判断对象是否为数组 * @param {Object} source 待判断的对象 * @return {Boolean} true|false */ Object. ...
 - Object.prototype.toString.call() 区分对象类型(判断对象类型)
		
在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种.对于数组. ...
 - 利用Object.prototype.toString方法,实现比typeof更准确的type校验
		
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...
 - Object.prototype.toString.call()为什么可以用来检测数据类型?
		
obj.toString()方法是用来干什么的 每一个对象都有一个toString()方法,默认情况下toString()被每一个Object对象继承,如果此方法未被重写,toString()返回&q ...
 - 从toString()方法到Object.prototype.toString.call()方法
		
一.toString方法和Object.prototype.toSting.call()的区别 var arr=[1,2]; 直接对一个数组调用toString()方法, console.log(ar ...
 - 关于toString()和valueOf()以及Object.prototype.toString.call()的结合理解
		
一.先说说String(): String()是全局函数,把对象的值转换为字符串. 语法:String(obj); 任何值(对象)都有String()方法,执行过程是这样的:首先,如果该对象上有toS ...
 - Object.prototype.toString.call(arg)详解
		
经常能碰到Object.prototype.toString.call对参数类型进行判断,一开始只知道怎么使用,却不了解具体实现的原理,最近恶补了一下相关知识,写个笔记加强理解,有什么不对的请指教. ...
 - 前端面试题1:Object.prototype.toString.call() 、instanceof 以及 Array.isArray()三种方法判别数组的优劣和区别
		
1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object ...
 
随机推荐
- mysql 排序后获得某行的位置
			
假设有test表,下图为表机构和数据,score表示积分.现在要查询积分排名为第几的id?? 查询语句 select id,score,(@rowno:=@rowno+1) as rowno from ...
 - shell命令一行代码搞定【转】
			
查看文件内容-while: cat 1.txt|while read line;do echo $line;done while read line; do echo $line; done < ...
 - WPF之让ListView中的CheckBox居中显示
			
第一步:在资源中定义一个居中的样式: <Window.Resources> <Style x:Key="ListViewItemStyle" TargetType ...
 - centos7如何关闭防火墙
			
1.centos7自带了firewall,而不是iptables: 关闭firewall: service firewalld stop 或者: systemctl stop firewalld 禁止 ...
 - js里父页面与子页面的相互调用
			
一.在页面里用 open 打开的子页面: 1.子页面调用父页面的方法,包括子页面给父页面传值: window.opener.methodName(); window.opener.methodName ...
 - 用 Swift 编写面向协议的网络请求
			
和我一起参加9 月 1 日 - 9月 2 日在纽约举办的 Swift 社区庆典
 - NDK Build 用法(NDK Build)
			
1.ndk-build的用法 Android NDKr4引入了一个新的.小巧的shell脚本ndk-build,来简化源码编译. 该文件位于NDK根目录,进入你的工程根目录或子目录之后,在命令行下调用 ...
 - java 对象流
			
TV.java import java.io.*; public class TV implements Serializable{ String name; int price; public vo ...
 - seo步骤
			
1. 关键词.找trends主词.去渣渣.(扩展).(去重tool sort) .打乱 :https://adwords.google.com/https://www.google.com/trend ...
 - AJAX(XMLHttpRequest)进行跨域请求方法详解(一)
			
注意:以下代码请在Firefox 3.5.Chrome 3.0.Safari 4之后的版本中进行测试.IE8的实现方法与其他浏览不同. 跨域请求,顾名思义,就是一个站点中的资源去访问另外一个不同域名站 ...