首页 > 其他分享 >js 判断变量是否为对象

js 判断变量是否为对象

时间:2022-12-14 19:23:20浏览次数:47  
标签:object console 变量 对象 Object js var isPlainObject log

使用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());

 图一

 

 

 

 

标签:object,console,变量,对象,Object,js,var,isPlainObject,log
From: https://www.cnblogs.com/xuey/p/16982674.html

相关文章

  • Qt平台下使用QJson 使用
    前言在Qt开发环境下使用Json的解析和输出当然要使用QJson来完成。QJson解析JSON主要使用的类如下#include<QJsonDocument>#include<QJsonObject>#include<QJsonArray>......
  • js60秒倒计时防刷新
    JavaScript倒计时,实现起来不难,但是一刷新往往就重新计算了,如果要实现刷新不重计该如何做呢?有这么几种思路,    1:cookie    2:本地缓存    3:window.name……......
  • leaflet 使用kriging-contour.js 与 turf.js生成等值面
    效果如下:leaflet生成等值面网上搜索了好多资料但测试感觉都有点瑕疵,kriging.js 生成的canvas图片每个都是小方格影响美感,turf.js会有非法Polygon且有白色缝隙。就想着可......
  • Java基础之变量
    变量变量为可以变化的量。java是一种强类型语言,每个变量都必须声明其类型。Java变量是程序中最基本的存储单位,其要素包括:变量名,变量类型和作用域。 数据类型变量名=......
  • CSS变量
    /*全局变量保存的地方*/:root{--main-bg-color:red;/*变量名必须以--开头*/}var(custom-property-name,value)值描述custom-property-name必需。自定......
  • 第一百一十三篇: JS数组Array(二)数组方法 栈、队列、排序
    好家伙,  在上一篇中,我们知道了,JS的数组中每个槽位可以存储任意类型的数据那么,我们能通过数组去模仿某些数据结构吗?答案是肯定的 1.栈方法ECMAScript给数组......
  • JS使用递归将原始数据转换为树形结构数据
    因为数据库中存放的数据终究全是扁平化的,因此获取后要手动将其改成树形结构,方便el-tree进行渲染。假设数据如下(至少是要有节点ID和父节点ID)   最终要达到如下效果(e......
  • webgl(three.js)3D光伏,3D太阳能能源,3D智慧光伏、光伏发电、清洁能源三维可视化解决
    3D太阳能、三维光伏、三维储能、使用three.js(webgl)搭建智慧楼宇、3D园区、3D厂房、3D仓库、设备检测、数字孪生、物联网3D、物业3D监控、物业基础设施......
  • vscode运行js文件
    一、首先你需要下载安装​​nodejs​​​​​下载地址​​二、在VSCode中有一个插件​​coderunner​​​,安装后可以直接运行在​​node​​环境中,然后就可以在vsc......
  • omitjs简单使用
    欢迎关注前端早茶,与广东靓仔携手共同进阶​前端早茶专注前端,一起结伴同行,紧跟业界发展步伐~1、omitjs干什么用的返回一个没有列入排除key属性的对象。其中,参数object为JSON......