How to get the object type in javascript:

来源:互联网 发布:网络翻译英文兼职 编辑:程序博客网 时间:2024/06/05 18:25

var obj = {}; // any object

1. typeof:

typeof obj; // object

2. instanceof:

obj instanceof Object; // true

3. Object.prototype.toString:

Object.prototype.toString.call(obj); // [object Object]

4. constructor:

obj.constructor // Object

5. Duck:

typeof obj === "object" && "hasOwnProperty" in obj && "toString" in obj; // true

 

Reference Material: http://www.iteye.com/topic/318821

 

原创粉丝点击