• js中类型判断


    JS判断数据类型

    js基本类型有 Undefined、Null、Boolean、Number和String,Object(由名值对集合)
    

    typeof判断类型

       var nullValue = null,boolValue = true,nmValue=12,strValue = 'test';
       var objValue={x:y},arrayValue = [1,23,3],
       functionValue = function(){
           console.info('function')
       };
       typeof nullValue == 'object';//true
       typeof boolValue == 'boolean';//true
       typeof nmValue == 'number';//true
       typeof strValue  == 'string';//true
       typeof objValue== 'object';//true
       typeof unValue== 'undefined';//true
       typeof arrayValue == 'object';//true
       typeof functionValue == 'function';//true
       typeof NaN//"number"
       /*
       *typeof 只能准确判断Undefined、Boolean、Number和String类型和function
       *对Null,Object,Array测试其值为'object'
       **/

    instanceof判断

    instanceof 判断已知对象,主要用于判断Array,object等

      [1,2]  instanceof Array;//true 数组
      new Date() instanceof Date;//Data
      var Obj = function(){}
      Obj.prototype.info = function(){
        console.info(Array.prototype.slice.apply(argument,[1]))
      }
      var testObj = new Obj();
      testObj instanceof Obj;//true  自定义对象
      var fucObj = function(){};
      fucObj  instanceof Function;//true 函数判断
      /*
      *instanceof 适合对象判断
      **/

    constructor判断

    适用于对象的判断

       false.constructor === Boolean;//true
       'test'.constructor === String;//true
       [1,23].constructor === Array;//true  
       var obj = {x:1}
       obj .constructor === Object;//true
      var funtest = function(){}
      funtest .constructor === Function;//true
      new Date().constructor  === Date;//true
      //对于继承
      var A = function(){};
      var B = function(){};
      A.prototype = new B();
      var a = new A();
      a.constructor === B;//true
      a.constructor === A;//false
      /*
      *此处对于instanceof 能胜任判断
      **/

    Object.prototype.toString.apply()判断

      "[Object Array]" === Object.prototype.toString.apply([]);//true
      "[object Object]"=== Object.prototype.toString.apply({});//true
      "[object String]" === Object.prototype.toString.apply('123');//true
      "[object Number]" === Object.prototype.toString.apply(123);//true
      "[object Boolean]" === Object.prototype.toString.apply(false);//true
      "[object Date]" === Object.prototype.toString.apply(new Date());//
      "[object Null]" === Object.prototype.toString.apply(null);//true
      "[object Undefined]" ===  Object.prototype.toString.apply(undefined);//true
  • 相关阅读:
    Linux内核基础--事件通知链(notifier chain)good【转】
    10 个迅速提升你 Git 水平的提示【转】
    notifier chain — 内核通知链【转】
    内核通知链 学习笔记 【转】
    Linux内核基础--事件通知链(notifier chain)【转】
    Git 使用规范流程【转】
    Linux中断(interrupt)子系统之二:arch相关的硬件封装层【转】
    学习 Linux,101: 自定义或编写简单脚本【转】
    MySQL数据处理函数
    Effective JavaScript Item 36 实例状态仅仅保存在实例对象上
  • 原文地址:https://www.cnblogs.com/jcheng996/p/5401480.html
Copyright © 2020-2023  润新知