js 判斷變量是否爲對象

使用toString()

let obj = {}
 
var res1 = Object.prototype.toString.call(obj) === '[object Object]'
console.log(res1); //true
 
var res2 = Object.prototype.toString.call(obj); 
console.log(res2); //[object Object] 
 1 var a = NaN;
 2 var b= '222';
 3 var c = null;
 4 var d = false;
 5 var e = undefined;
 6 var f = Symbol(); 
 7 var arr = ['aa','bb','cc'];
 8 var obj = { 'a': 'aa', 'b': 'bb', 'c': 'cc' }; 
 9 var res = Object.prototype.toString.call(arr);
10 console.log(res); 
11 //[object Array] 
12 var res2 = Object.prototype.toString.call(obj); 
13 console.log(res2); //[object Object] 
14 var res3 = Object.prototype.toString.call(a);
15  console.log(res3); //[object Number] 
16  var res4 = Object.prototype.toString.call(b); 
17  console.log(res4); //[object String] 
18  var res4 = Object.prototype.toString.call(c); 
19  console.log(res4); //[object Null] 
20  var res5 = Object.prototype.toString.call(d); 
21  console.log(res5); //[object Boolean] 
22  var res6 = Object.prototype.toString.call(e); 
23  console.log(res6); //[object Undefined] 
24  var res7 = Object.prototype.toString.call(f); 
25  console.log(res7); //[object Symbol]
26  // JavaScript Document
View Code

constructor

var arr = ['aa','bb','cc'];
var obj = {
'a': 'aa',
'b': 'bb',
'c': 'cc'
};
console.log(arr.constructor === Array); //true
console.log(arr.constructor === Object); //false
console.log(obj.constructor === Object); //true

instanceof

var arr = new Array();
var arr = ['aa','bb','cc'];
var obj = { a: 'aa', b: 'bb', c: 'cc' };

console.log(arr instanceof Array); //true
console.log(arr instanceof Object); //true
console.log(obj instanceof Array); //false
console.log(obj instanceof Object); //true

注意:數組也是對象的一種

typeof判斷變量的類型

// 根據typeof判斷對象也不太準確
 
//表達式                       返回值
typeof undefined//           'undefined'
typeof null  //              'object'
typeof true       //         'boolean' 
typeof 123        //         'number'
typeof "abc"       //        'string' 
typeof function() {} //      'function' 
typeof {}             //     'object'
typeof []             //     'object'

$.isPlainObject()

判斷指定參數是否是一個純粹的對象(所謂"純粹的對象",就是該對象是通過"{}"或"new Object"創建的。)

let obj={};
$.isPlainObject(obj);
 1 $.isPlainObject({}); // true
 2 $.isPlainObject(new Object()); // true
 3 $.isPlainObject({ name: "CodePlayer" }); // true
 4 $.isPlainObject({ sayHi: function () { } }); // true
 5 
 6 $.isPlainObject("CodePlayer"); // false
 7 $.isPlainObject(true); // false
 8 $.isPlainObject(12); // false
 9 $.isPlainObject([]); // false
10 $.isPlainObject(function () { }); // false
11 $.isPlainObject(document.location); // false(在IE中返回true)
12 
13 function Person() {
14         this.name = "張三";
15 }
16 $.isPlainObject(new Person()); // false
View Code
var object_type = new Object();//見 圖1  $.isPlainObject(object_type) //true
var json_type = { name: 123 };//見 圖2  $.isPlainObject(json_type) //true
var Person = function () {this.age = 15;} //見 圖3  $.isPlainObject(Person) //false
console.log(object_type);
console.log(json_type);
console.log(new Person());

 圖一

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章