• js的toString方法


    专栏目录请点击

    toString()参数为字符串的转换基数 点击

    数据类型

    undefinednull

    一般undefinednull,没有toString()方法,但是从ES6之后规定:null返回[object Null]undefined返回[object Undefined],当然,我们也可以用来检测一些其他的类型 点击

    数组

    数组调用toString,返回一个字符串,他是数组中的元素 点击

    // 数组
    const arr = [1,2,3]
    console.log(arr.toString()) // 1,2,3
    
    • 1
    • 2
    • 3

    布尔值

    布尔型数据true和false返回对应的’true’和’false’

    字符串

    字符串类型原值返回

    数值

    1. 正浮点数及NaN、Infinity加引号返回,如果有-号,那么他会先执行toString方法,然后执行-号的运算
    1.23.toString();//'1.23'
    typeof 1.23.toString(); //string
    NaN.toString();//'NaN'
    Infinity.toString();//'Infinity'
    -1.23.toString(); // -1.23
    typeof -1.23.toString() // number
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 整数调用,如果整数连续调用,就必须加上括号,不然的话.会被当做小数点而报错
    // 整数
    0.toString() // 错误的写法
    (0).toString() // 0
    
    • 1
    • 2
    • 3

    当然,我们在写的时候,编辑器也会给我们报错在这里插入图片描述

    对象

    会得到[object Object]的字符串

    const obj = {}
    
    console.log(obj.toString()) // [object Object]
    
    • 1
    • 2
    • 3

    类型判断

    console.log(Object.prototype.toString.call("jerry"));//[object String]
    console.log(Object.prototype.toString.call(12));//[object Number]
    console.log(Object.prototype.toString.call(true));//[object Boolean]
    console.log(Object.prototype.toString.call(undefined));//[object Undefined]
    console.log(Object.prototype.toString.call(null));//[object Null]
    console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
    
    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(/\d/));//[object RegExp]
    function Person(){};
    console.log(Object.prototype.toString.call(new Person));//[object Object]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    类型识别函数

    所以我们可以定义一个函数来进行类型的识别

    function type(obj){
        return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();
    }
    console.log(type("jerry"));//"string"
    console.log(type(12));//"number"
    console.log(type(true));//"boolean"
    console.log(type(undefined));//"undefined"
    console.log(type(null));//"null"
    console.log(type({name: "jerry"}));//"object"
    
    console.log(type(function(){}));//"function"
    console.log(type([]));//"array"
    console.log(type(new Date));//"date"
    console.log(type(/\d/));//"regexp"
    function Person(){};
    console.log(type(new Person));//"object"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    其他的识别功能

    arguments与DOM

    (function(){
        console.log(Object.prototype.toString.call(arguments));//[object Arguments]
    })()
    console.log(Object.prototype.toString.call(document));//[object HTMLDocument]
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    自定义函数与内置函数

    1. 自定义函数会得到函数的源码
    2. 内置函数会有native code的字符串
    function fn(){
        console.log("hello world");
    }
    
    console.log(fn.toString())
    console.log(Function.toString())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    function fn(){
        console.log("hello world");
    }
    function Function() { [native code] }
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    阿里业务平台技术质量部——测试开发面试
    C++11 Forward
    Redis 中常见的集群部署方案
    常用稳压电源---DCDC和LDO
    2022年陕西省工程师职称申报,基本的职称论文要求您得知道啊
    设计模式——享元模式
    Python学习笔记1:reverse()函数和reversed()函数
    利用bert4keras实现多任务学习
    python内置函数+lambda函数
    Android中focusableInTouchMode会导致第一次点击事件失效
  • 原文地址:https://blog.csdn.net/youhebuke225/article/details/126065650