zoukankan      html  css  js  c++  java
  • 判断数据类型的5种方法

    1. typeof

    • 可以判断数据类型,它返回表示数据类型的字符串(返回结果只能包括number,boolean,string,function,object,undefined);
    • 可以使用typeof判断变量是否存在(如if(typeof a!="undefined"){...});
    • Typeof 运算符的问题是无论引用的对象是什么类型 它都返回object
    typeof {} // object
    typeof  [1,2] // object
    typeof /s/ //object

    2.instanceof

    原理 因为A instanceof B 可以判断A是不是B的实例,返回一个布尔值,由构造类型判断出数据类型

    console.log(arr instanceof Array ); // true
    console.log(date instanceof Date ); // true
    console.log(fn instanceof Function ); // true
    //注意: instanceof 后面一定要是对象类型,大小写不能写错,该方法试用一些条件选择或分支

    3.通过Object下的toString.call()方法来判断

    Object.prototype.toString.call();
    console.log(toString.call(123)); //[object Number]
    console.log(toString.call('123')); //[object String]
    console.log(toString.call(undefined)); //[object Undefined]
    console.log(toString.call(true)); //[object Boolean]
    console.log(toString.call({})); //[object Object]
    console.log(toString.call([])); //[object Array]
    console.log(toString.call(function(){})); //[object Function]

    4.根据对象的contructor判断

    console.log('数据类型判断' -  constructor);
    console.log(arr.constructor === Array); //true
    console.log(date.constructor === Date); //true
    console.log(fn.constructor === Function); //true

    5.jq中判断数据类型的方法

    jQuery提供了一系列工具方法,用来判断数据类型,以弥补JavaScript原生的typeof运算符的不足。以下方法对参数进行判断,返回一个布尔值。
    jQuery.isArray();是否为数组
    jQuery.isEmptyObject();是否为空对象 (不含可枚举属性)。
    jQuery.isFunction():是否为函数
    jQuery.isNumberic():是否为数字
    jQuery.isPlainObject():是否为使用“{}”或“new Object”生成对象,而不是浏览器原生提供的对象。
    jQuery.isWindow(): 是否为window对象;
    jQuery.isXMLDoc(): 判断一个DOM节点是否处于XML文档中。


    作者:8d2855a6c5d0
    链接:https://www.jianshu.com/p/967d6db70437
    来源:简书
  • 相关阅读:
    Ext.form.TextField组件
    provider: SQL Network Interfaces, error: 26 Error Locating Server/Instance Specified解决办法
    SQL Server访问远程数据库和Linked Server
    C#程序实现动态调用DLL的研究
    把DLL文件打包进EXE的技巧
    C#读取媒体信息
    C# 集合类
    Server Application Unavailable 【Failed to execute request because the AppDomain could not be created.】的解决办法
    比较全的字符串验证类
    如何用VS2005制作Web安装程序
  • 原文地址:https://www.cnblogs.com/passkey/p/12757589.html
Copyright © 2011-2022 走看看