zoukankan      html  css  js  c++  java
  • js数据类型

    以下摘自慕课网Bson老师的课程,这里只是自己做的笔记:

    1 JavaScript六种数据类型:
    number string boolean null undefined Object
    2 JavaScript包装对象:
    六中数据类型中有3种 原始类型,分别是:number string boolean,它们都有自己的包装类型,分别是:Number,String, Boolean
    例如:
    var strObj=new String("string");
    var j=new Number(5);
    var bo2=new Boolean(true);
    3 js类型检测:
    类型检测有如下方法:typeof , instanceof , Object.protoptype.toString , constructor, duck type;
    3.1 typeof
    typeof :返回一个字符串,它非常适合函数类型和基本类型的判断,如下:
    typeof 100 // "number"
    typeof NaN //"number" 注:NaN===NaN false
    typeof true // "boolean"
    function aaa(){};
    typeof aaa // "function"
    typeof undefined //"undefined"
    typeof new Object() //"object"
    typeof [1,2] //"object"
    typeof null //"object"
    3.2 instanceof
    instanceof 用于判断对象类型,比如:数组,日期
    instanceof 是基于原型链的进行操作的一个运算符,它期望左边是一个对象,如果不是对象,则直接返回false,期望右边是一个函数对象或者是一个函数构造器,如果不是,则会抛出一个TypeError异常。instanceof 判断的原理是:左操作数的这样一个对象的原型链上是否有右边这个构造函数的
    注意:不同window或iframe间的对象类型检测不能使用instanceof!
    prototype属性
    var a=new Array();
    a instanceof Array //true
    12 instanceof Number //false
    var num=new Number(12);
    num instanceof Number // true
     
    function person(){};
    function student(){};
    student.prototype=new person();
    student instanctof person //true;
     
    3.3 Object.prototype.toString.apply()
    Object.prototype.toString.apply([]); //"[object Array]
    Object.prototype.toString.apply(function(){}); //"[object Function]"
    Object.prototype.toString.apply(null); //null;
    注意:IE6/7/8 Object.prototype.toString.apply(null); 返回“[Object Object]”
     
    3.4 constructor
    任何一个对象都有constructor属性,是继承自原型的,这个constructor会指向这个构造对象的构造器(构造函数),由于constructor可以被改写的,所以使用需要小心。
    3.5 duck type
    比如我们不知道这个对象是否是数组,我们可以判断它的 length属性是否是数字,它是否有join、push这样的函数(方法)
    3.6 js类型检测小结
    typeof:
    适合基本类型及function检测,遇到null失效。
    可以使用严格等于判断是否为null。(===)
    Object.prototype.toString.apply():
    通过{}.toString拿到,适合内置对象和基本元素遇到null和undefined失效(IE678等返回[object Object])。
    instanceof:
    适合自定义对象,也可以用来检测原生对象,在不同iframe和window间检测时失效。
    ===================实现判断两个数组的内的类型是否相同==========================
    function arraysSimilar(arr1, arr2) {
        if (!(arr1 instanceof Array) || !(arr2 instanceof Array)) {
            return false;
        }
        if (arr1.length !== arr2.length) return false;
        var i = 0, n = arr1.length,
            countMap1 = {},
            countMap2 = {},
            t1, t2,
            TYPES = ['string', 'boolean', 'number', 'undefined', 'null', 'function', 'date', 'window'];
        for (; i < n; i++) {
            t1 = typeOf (arr1[i]);
            t2 = typeOf (arr2[i]);
            if (countMap1[t1]) {
                countMap1[t1]++;
            } else {
                countMap1[t1] = 1;
            }
            if (countMap2[t2]) {
                countMap2[t2]++;
            } else {
                countMap2[t2] = 1;
            }
        }
        function typeOf(ele) {
            var r;
            if (ele === null) r = 'null';
            else if (ele instanceof Array) r = 'array';
            else if (ele === window) r = 'window';
            else if (ele instanceof Date) r = 'date';
            else r = typeof ele;
            return r;
        }
        for (i = 0, n = TYPES.length; i < n; i++) {
            if (countMap1[TYPES[i]] !== countMap2[TYPES[i]]) {
                return false;
            }
        }
        return true;
    }
    判断两个数组是否相似

    ==============调用测试==============

    <script>
        $(function () {
            var result = function () {
                //以下为多组测试数据
                var cases = [{
                    arr1: [1, true, null],
                    arr2: [null, false, 100],
                    expect: true
                }, {
                    arr1: [function () { }, 100],
                    arr2: [100, {}],
                    expect: false
                }, {
                    arr1: [null, 999],
                    arr2: [{}, 444],
                    expect: false
                }, {
                    arr1: [window, 1, true, new Date(), "hahaha", (function () { }), undefined],
                    arr2: [undefined, (function () { }), "okokok", new Date(), false, 2, window],
                    expect: true
                }, {
                    arr1: [new Date()],
                    arr2: [{}],
                    expect: false
                }, {
                    arr1: [window],
                    arr2: [{}],
                    expect: false
                }, {
                    arr1: [undefined, 1],
                    arr2: [null, 2],
                    expect: false
                }, {
                    arr1: [new Object, new Object, new Object],
                    arr2: [{}, {}, null],
                    expect: false
                }, {
                    arr1: null,
                    arr2: null,
                    expect: false
                }, {
                    arr1: [],
                    arr2: undefined,
                    expect: false
                }, {
                    arr1: "abc",
                    arr2: "cba",
                    expect: false
                }];
    
                //使用for循环, 通过arraysSimilar函数验证以上数据是否相似,如相似显示“通过”,否则"不通过" 
                for (var i = 0; i < cases.length; i++) {
                    if (arraysSimilar(cases[i].arr1, cases[i].arr2) !== cases[i].expect) {
                        document.write("不通过!case" + (i + 1) + "不正确!arr1=" + JSON.stringify(cases[i].arr1) + ", arr2=" + JSON.stringify(cases[i].arr2) + " 的判断结果不是" + cases[i].expect);
                        return false;
                    }
                }
                return true;
    
            }();
            document.write("判定结果:" + (result ? "通过" : "不通过"));
        });
          
            
    
    </script>
    View Code

  • 相关阅读:
    Spring + SpringMVC + MyBatis
    jquery+bootstrap使用数字增减按钮
    Eclipse添加代码注释模板
    No goals have been specified for this build
    字符串前面自动补零
    深入理解JavaScript系列
    java判断A字符串是否包含B字符串
    WebSocket 实战
    button点击切换,获取按钮ID
    JS 中判断空值 undefined 和 null
  • 原文地址:https://www.cnblogs.com/Torey/p/10125934.html
Copyright © 2011-2022 走看看