zoukankan      html  css  js  c++  java
  • 关于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
    
  • 相关阅读:
    To select the file to upload we can use the standard HTML input control of type
    Cascading Menu Script using Javascript Explained
    网站首页head区代码规范
    轻松掌握 Java 泛型
    JDK 5.0 中的泛型类型学习
    如何在firefox下获取下列框选中option的text
    是同步方法还是 synchronized 代码? 详解多线程同步规则
    javascript select option对象总结
    Select的动态取值(Text,value),添加,删除。兼容IE,FireFox
    javascript在ie和firefox下的一些差异
  • 原文地址:https://www.cnblogs.com/Alencong/p/5861610.html
Copyright © 2011-2022 走看看