判断变量是否为数组及通用判断数据类型方法

判断变量是不是数组类型

 function fn() {
                console.log(Array.isArray(arguments));   //false; 因为arguments是类数组,但不是数组
                console.log(Array.isArray([1,2,3,4]));   //true
                console.log(arguments instanceof Array); //fasle
                console.log([1,2,3,4] instanceof Array); //true
                console.log(Object.prototype.toString.call(arguments)); //[object Arguments]
                console.log(Object.prototype.toString.call([1,2,3,4])); //[object Array]
                console.log(arguments.constructor === Array); //false
                arguments.constructor = Array;
                console.log(arguments.constructor === Array); //true
                console.log(Array.isArray(arguments));        //false
            }
            fn(1,2,3,4);

通用判断数据类型方法
Object.prototype.toString.call()所有原始数据类型都能通过该方法判断,具有通用性

				Object.prototype.toString.call(arr); // "[object Array]"
                Object.prototype.toString.call(2); // "[object Number]"
                Object.prototype.toString.call(""); // "[object String]"
                Object.prototype.toString.call(true); // "[object Boolean]"
                Object.prototype.toString.call(undefined); // "[object Undefined]"
                Object.prototype.toString.call(null); // "[object Null]"
                Object.prototype.toString.call(Math); // "[object Math]"
                Object.prototype.toString.call({}); // "[object Object]"
                Object.prototype.toString.call([]); // "[object Array]"
                Object.prototype.toString.call(function () {}); // "[object Function]"
            }
DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>判断变量是否为数组方法title>
    <script type="text/javascript">
            window.onload = function () {
            function fn() {
                console.log(Array.isArray(arguments));   //false; 因为arguments是类数组,但不是数组
                console.log(Array.isArray([1,2,3,4]));   //true
                console.log(arguments instanceof Array); //fasle
                console.log([1,2,3,4] instanceof Array); //true
                console.log(Object.prototype.toString.call(arguments)); //[object Arguments]
                console.log(Object.prototype.toString.call([1,2,3,4])); //[object Array]
                console.log(arguments.constructor === Array); //false
                arguments.constructor = Array;
                console.log(arguments.constructor === Array); //true
                console.log(Array.isArray(arguments));        //false
            }
            fn(1,2,3,4);

            //Object.prototype.toString.call()所有原始数据类型都能通过该方法判断,具有通用性
                Object.prototype.toString.call(arr); // "[object Array]"
                Object.prototype.toString.call(2); // "[object Number]"
                Object.prototype.toString.call(""); // "[object String]"
                Object.prototype.toString.call(true); // "[object Boolean]"
                Object.prototype.toString.call(undefined); // "[object Undefined]"
                Object.prototype.toString.call(null); // "[object Null]"
                Object.prototype.toString.call(Math); // "[object Math]"
                Object.prototype.toString.call({}); // "[object Object]"
                Object.prototype.toString.call([]); // "[object Array]"
                Object.prototype.toString.call(function () {}); // "[object Function]"
            }
    script>
head>
<body>
body>
html>

判断变量是否为数组及通用判断数据类型方法_第1张图片

你可能感兴趣的:(javascript,前端)