• 关于javascript 里面类型的判断


    javacript至今共有7中类型
    Six data types that are primitives:

    1. Boolean
    2. Null
    3. Undefined
    4. Number
    5. String
    6. Symbol (new in ECMAScript 6)
    7. Object

    关于typeof的详情解释

    demo1:

    Function.prototype.toString() 与 Object.prototype.toString()的区别:

    
    var f = function(){};
    f.prototype.hasOwnProperty("toString") ; //=>false
    Function.prototype.hasOwnProperty("toString"); //=>true
    
    
    Object.prototype.toString.call(f); //=>[object Function]
    
    Function.prototype.toString(f);  //=>"function () {}"
    
    Object.toString.call(f) ;  //=>"function () {}"
    
    //注:Object 是由Function构造的,所以Object.toString会这样 ,由Object.__proto__.toString 遍历原型链去找到Function.prototype.toString() 打印,所以会打印function f(){}
    
    //jquery 类型的判断,demo2
    {}.toString.call(f);  //=>[object Function]
    
    
    

    所以,所有由Object.prototype.toString 方法调用的对象,都是会打印出[object Function],[object String] 或者其他类似的格式,而这个结果也常常会被我们用来去判断这是否是一个String 字符串,Array ...

    Object.prototype.toString.call([]) === "[object Array]"  // ==>"[object Array]"
    
    Object.prototype.toString.call({}) === "[object Array]"  // ==>"[object Object]"
    
    demo2:

    jQuery.type() 方法的源码解析:

    var class2type = {};
    
    // Populate the class2type map
    /* 覆盖class2type: 用做返回的的类型
      var class2type = {
        "[object Boolean]":"boolean",
        "[object Number]":"number",
        "[object String]":"string",
        "[object Function]":"function",
        "[object Array]":"array",
        "[object Date]":"date",
        "[object RegExp]":"regexp",
        "[object Object]":"object",
        "[object Error]":"error"
    }
    */
    jQuery.each( "Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " ),
    function( i, name ) {
    	class2type[ "[object " + name + "]" ] = name.toLowerCase();
    } );
        ...
        type: function( obj ) {
            //  return null
        	if ( obj == null ) {
        		return obj + "";
        	}
        
        	// Support: Android <=2.3 only (functionish RegExp)
        	// 支持: Android <= 2.3 版本  ?(functionish RegExp)
        	// 如果是个函数的话,返回class2type[{}.toString.call(obj)]
        	return typeof obj === "object" || typeof obj === "function" ?
        		class2type[ toString.call( obj ) ] || "object" :
        		typeof obj;
        },
        ...
    
    

    On IE 6, 7, and 8 a lot of host objects are objects and not functions. For example: typeof alert === 'object'

    This stnds since the beginning of JavaScript typeof null === 'object';

    Regular expressions

    在一些正则中,低版本的浏览器可能会不兼容es5

    typeof /s/ === 'function'; // Chrome 1-12 不遵循  5.1
    typeof /s/ === 'object';   // Firefox 5+  遵循 ECMAScript 5.1
    
  • 相关阅读:
    30秒懂SQL中的join(2幅图+30秒)
    MySQL字符串连接函数
    php 月初,月末时间大统计
    php判断中文,英文, 数字
    配置linux----------------ip
    配置samba
    Python的MySQLdb模块安装
    python 之 PIP 安装
    linux之svn
    vue之computed(计算属性)
  • 原文地址:https://www.cnblogs.com/Alencong/p/5861610.html
Copyright © 2020-2023  润新知