zoukankan      html  css  js  c++  java
  • javascript typeof和instanceof

     一、

     基本数据类型:字符串、数字、布尔、Null、Undefined、symbol(ES6)
     引用数据类型:对象、数组、Function等。

     二、typeof它返回值是一个字符串,该字符串说明运算数的类型。

    typeof (123) //"number"
    typeof ("123") // "string"

    typeof (true) // "boolean"

    typeof (Symbol()) // "symbol"

    typeof (undefined) // "undefined"
    typeof (a) // "undefined" a未定义

    typeof (function(){}) // "function"

    typeof ({}) // "object"

    typeof ([])  // "object"
    typeof (null) // "object"

    三、正确的判断数据类型(通用方法)

    Object.prototype.toString.call(123); // "[object Number]"
    Object.prototype.toString.call("123"); // "[object String]"
    Object.prototype.toString.call(true); // "[object Boolean]"
    Object.prototype.toString.call([]); // "[object Array]"
    Object.prototype.toString.call(function(){}); // "[object Function]"
    Object.prototype.toString.call(undefined); // "[object Undefined]"
    Object.prototype.toString.call(null); // "[object Null]"
    Object.prototype.toString.call(new Date()); // "[object Date]"
    Object.prototype.toString.call(/^1$/); // "[object RegExp]"
    Object.prototype.toString.call(new Error()); // "[object Error]"

    四、instanceof运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上

      【类型判断】

    [] instanceof Array // true
    new Date instanceof Date // true
    function(){} instanceof Function // true

    附:

    原型链:https://www.cnblogs.com/DF-fzh/p/5619319.html






  • 相关阅读:
    MATLAB矩阵操作【z】
    matlab绘图方法[z]
    Drawhere 有趣的网页涂鸦工具【z】
    DemoHelper,针对电脑演示的小工具
    Matlab Matrix [z]
    MATLAB函数参考[z]
    计算几何常用算法概览[z]
    matlab命令行环境的常用操作[z]
    不常见数学符号或简写
    matlab加入上级路径和本级路径的方法
  • 原文地址:https://www.cnblogs.com/baoliwei/p/8341836.html
Copyright © 2011-2022 走看看