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

    Js获取数据类型

    JavaScript有着七种基本类型StringNumberBooleanNullUndefinedSymbolObject,前六种为基本数据类型,Object为引用类型。

    typeof

    typeof(operand)typeof operandoperand是一个表示对象或原始值的表达式,其类型将被返回。

    规则

    • String: "string"
    • Number: "number"
    • Boolean: "boolean"
    • Null: "object"
    • Undefined: "undefined"
    • Symbol: "symbol"
    • BigInt: "bigint"
    • Function Object: "function"
    • Object: "Object"

    示例

    console.log(typeof(a)); // undefined // 未定义的变量
    console.log(typeof("s")); // string
    console.log(typeof(1)); // number
    console.log(typeof(true)); // boolean
    console.log(typeof(new String("s"))); // object
    console.log(typeof(new Number(1))); // object
    console.log(typeof(new Boolean(true))); // object
    console.log(typeof(null)); // object // 在 JavaScript 最初的实现中,JavaScript 中的值是由一个表示类型的标签和实际数据值表示的。对象的类型标签是 0。由于 null 代表的是空指针(大多数平台下值为 0x00),因此,null 的类型标签是 0,typeof null 也因此返回 "object"。
    console.log(typeof(undefined)); // undefined
    console.log(typeof(Symbol())); // symbol
    console.log(typeof(Object(Symbol()))); // object
    console.log(typeof(1n)); // bigint // ES10(ES2019)新增基本数据类型
    console.log(typeof(Object(BigInt(1n)))); // object
    console.log(typeof(function() {})); // function
    console.log(typeof([])); // object
    console.log(typeof(new Date())); // object
    console.log(typeof(/regex/)); // object
    console.log(typeof({})); // object
    

    instanceof

    instanceof运算符用于检测构造函数的prototype属性是否出现在某个实例对象的原型链上。在Js中,一切都是对象,至少被视为一个对象,能够直接使用字面量声明的基本数据类型,虽然并不是直接的对象类型,但是在基本数据类型的变量调用方法的时候,会出现一个临时的包装对象,从而能够调用其构造函数的原型的方法,所以使用instanceof时对于字面量声明的StringNumberBooleanSymbolBigInt都会返回false

    示例

    console.log("s" instanceof String); // false
    console.log(1 instanceof Number); // false
    console.log(true instanceof Boolean); // false
    console.log(new String("s") instanceof String); // true
    console.log(new Number(1) instanceof Number); // true
    console.log(new Boolean(true) instanceof Boolean); // true
    console.log(null instanceof Object); // false 
    console.log(undefined instanceof Object); // false
    console.log(Symbol() instanceof Symbol); // false
    console.log(Object(Symbol()) instanceof Symbol); // true
    console.log(1n instanceof BigInt); // false
    console.log(Object(1n) instanceof BigInt); // true
    console.log(Symbol() instanceof Symbol); // false
    console.log((function() {}) instanceof Function); // true
    console.log([] instanceof Array); // true
    console.log(new Date() instanceof Date); // true
    console.log(/regex/ instanceof RegExp); // true
    console.log({} instanceof Object); // true
    

    Object.prototype.toString

    借助Object原型的toString方法判断类型,一般对于新建的不同类型的对象toString方法都会被重新定义,无法沿着原型链到达Object.prototype.toString,可以通过call或者apply来调用Object.prototype.toString用以判断类型。

    示例

    console.log(Object.prototype.toString.call("s")); // [object String]
    console.log(Object.prototype.toString.call(1)); // [object Number]
    console.log(Object.prototype.toString.call(true)); // [object Boolean]
    console.log(Object.prototype.toString.call(new String("s"))); // [object String]
    console.log(Object.prototype.toString.call(new Number(1))); // [object Number]
    console.log(Object.prototype.toString.call(new Boolean(true))); // [object Boolean]
    console.log(Object.prototype.toString.call(null)); // [object Null] 
    console.log(Object.prototype.toString.call(undefined)); // [object Undefined]
    console.log(Object.prototype.toString.call(Symbol())); // [object Symbol]
    console.log(Object.prototype.toString.call(Object(Symbol()))); // [object Symbol]
    console.log(Object.prototype.toString.call(1n)); // [object BigInt]
    console.log(Object.prototype.toString.call(Object(BigInt(1n)))); // [object BigInt]
    console.log(Object.prototype.toString.call(function() {})); // [object Function]
    console.log(Object.prototype.toString.call([])); // [object Array]
    console.log(Object.prototype.toString.call(new Date())); // [object Date]
    console.log(Object.prototype.toString.call(/regex/)); // [object RegExp]
    console.log(Object.prototype.toString.call({})); // [object Object]
    

    每日一题

    https://github.com/WindrunnerMax/EveryDay
    

    参考

    https://www.cnblogs.com/sban/p/10256412.html
    https://www.cnblogs.com/yucheng6/p/9747313.html
    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures
    
  • 相关阅读:
    C++学习9 this指针详解
    福建省第八届 Triangles
    UVA 11584 Partitioning by Palindromes
    POJ 2752 Seek the Name, Seek the Fame
    UVA 11437 Triangle Fun
    UVA 11488 Hyper Prefix Sets (字典树)
    HDU 2988 Dark roads(kruskal模板题)
    HDU 1385 Minimum Transport Cost
    HDU 2112 HDU Today
    HDU 1548 A strange lift(最短路&&bfs)
  • 原文地址:https://www.cnblogs.com/WindrunnerMax/p/12836105.html
Copyright © 2011-2022 走看看