首页 > 其他分享 >巧用模板字符串将未知变量转换为string类型,避免报错

巧用模板字符串将未知变量转换为string类型,避免报错

时间:2023-10-21 15:36:19浏览次数:38  
标签:string 报错 字符串 null 模板 变量

可理解为将变量向字符串类型转换的语法糖用法

我们通常会遇到需要用String.prototype上的方法处理变量,如果该变量为null、undefined、Object则不能直接用字符串方法,也不易于统一处理为字符串;

使用模板字符串包裹该变量,则可以简单粗暴的将任意类型转换为字符串类型,避免报错。

案例:

const ces = `${[]}-${{}}-${undefined}-${null}-${0.0}`;

 

标签:string,报错,字符串,null,模板,变量
From: https://www.cnblogs.com/jiewanga/p/17779043.html

相关文章

  • Docker启动失败,报错Cannot connect to the Docker daemon at unix:///var/run/docker
    问题描述:CannotconnecttotheDockerdaemonatunix:///var/run/docker.sock.Isthedockerdaemonrunning?首次安装docker后,服务没有自启动解决方案:查看docker状态 1servicedockerstatus 设置开启Linux时docker自启动 1systemctlenabledocker   然后......
  • Docker启动报错:Cannot connect to the Docker daemon at unix:///var/run/docker.sock
    问题描述:  Docker启动报错:CannotconnecttotheDockerdaemonatunix:///var/run/docker.sock.Isthedockerdaemonrunning?  CannotconnecttotheDockerdaemonatunix:///var/run/docker.sock.Isthedockerdaemonrunning?输入dockerps命令无法显示服......
  • STL(标准模板库)
    以下是关于STL(标准模板库)的一个详细复习提纲,以帮助你温习相关知识点。序列容器vector定义和创建vectorvector的常用操作方法(例如插入、删除、访问元素等)vector的动态扩容机制vector的迭代器使用list定义和创建listlist的常用操作方法(例如插入、删除、访问元素等)list......
  • 快速排序算法模板+内置函数
    思想:确定分界点调整区间,小于分界点的在左边区间,大于分界点在右边区间。递归处理左右两边。voidquick_sort(intq[],intl,intr){if(l>=r)return;inti=l-1,j=r+1,x=q[l+r>>1];while(i<j){doi++;while(q[i......
  • 运用模板重载二维数组
    #include<iostream>#include<array>usingnamespacestd;//stack.htemplate<typenameT>classArray{public: Array(introw,intcol); T*operator[](introw);public: T*m_pT; intm_Row; intm_Col;};template<typenameT>Ar......
  • 04String类
    String类字符串是常量,创建之后不可改变。字符串字面值存储在字符串池中,可以共享。Stringstr="Hello";产生一个str对象,字符串Hello在字符串池(常量池)中存储。Stringstr1=newString("Hello");产生两个对象,堆、池里面各存储一个。str1指向堆里面的空间,堆里面指向......
  • sonarqube启动报错:You must address the points described in the following [2] line
    Youmustaddressthepointsdescribedinthefollowing[2]linesbeforestartingElasticsearch.bootstrapcheckfailure[1]of[2]:maxnumberofthreads[3870]foruser[sonar]istoolow,increasetoatleast[4096]bootstrapcheckfailure[2]of[2]:maxvi......
  • dolphinscheduler报错:Connect to 192.168.xx.xx:8088 [192.168.xx.xx/110.173.196.1]
    报错信息:在dophin中抽取mysql的数据到hive中报错[ERROR]2023-10-2015:33:10.461org.apache.dolphinscheduler.common.utils.HttpUtils:[73]-Connectto192.168.xx.xx:8088[192.168.xx.xx/110.173.196.1]failed:connecttimedoutorg.apache.http.conn.ConnectTimeout......
  • jpa 连接sqlserver 发布tomcat报错 SunJSSE
    报错信息:java.ext.dirs:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.332.b09-1.el7_9.x86_64/jre/lib/ext:/usr/java/packages/lib/ext20-Oct-202316:37:28.074信息[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0]com.microsoft.sqlserver.jdbc.TDSChanne......
  • xxl-job执行java任务报错: unable to find valid certification path to requested tar
    1、错误:xxl-job调用https接口显示证书验证失败[错误信息:sun.security.validator.ValidatorException:PKIXpathbuildingfailed:sun.security.provider.certpath.SunCertPathBuilderException:unabletofindvalidcertificationpathtorequestedtarget]2023-10-2015......