分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

判断js中的数据类型的方法

发布时间:2023-09-06 02:18责任编辑:顾先生关键词:js数据类型

在 判断js中的数据类型 我们通常会使用typeOf()方法,

       typeof   2         输出   number
       typeof   null       输出   object

       typeof   {}        输出   object

       typeof    []      输出   object

       typeof   (function(){}) 输出  function

       typeof    undefined  输出  undefined

       typeof   ‘222‘      输出    string

      typeof  true        输出     boolean

  

typeof  用来判断null ,对象 , 数组 都会返回 object,那我们怎么来区分他们呢?

下面就用到了

判断已知对象类型的方法: instanceof

  

var a = "iamstring.";
var b = 222;
var c= [1,2,3];
var d = new Date();
var e = function(){alert(111);};
var f = function(){this.name="22";};

alert(c instanceof Array) ---------------> truealert(d instanceof Date) alert(f instanceof Function) ------------> truealert(f instanceof function) ------------> false注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

无敌万能的方法:jquery.type()

如果对象是undefined或null,则返回相应的“undefined”或“null”。jQuery.type( undefined ) === "undefined"jQuery.type() === "undefined"jQuery.type( window.notDefined ) === "undefined"jQuery.type( null ) === "null"如果对象有一个内部的[[Class]]和一个浏览器的内置对象的 [[Class]] 相同,我们返回相应的 [[Class]] 名字。 (有关此技术的更多细节。 )jQuery.type( true ) === "boolean"jQuery.type( 3 ) === "number"jQuery.type( "test" ) === "string"jQuery.type( function(){} ) === "function"jQuery.type( [] ) === "array"jQuery.type( new Date() ) === "date"jQuery.type( new Error() ) === "error" // as of jQuery 1.9jQuery.type( /test/ ) === "regexp"其他一切都将返回它的类型“object”。
 

判断js中的数据类型的方法

原文地址:https://www.cnblogs.com/ysdemo/p/9782542.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved