本文共 832 字,大约阅读时间需要 2 分钟。
在很多情景下,需要对字符串进行判空的操作,例如表单提交或获取后端数值。
typeof是一个运算符,有2种使用方式:typeof(表达式)和typeof 变量名,第一种是对表达式做运算,第二种是对变量做运算。
var content;if(typeof content === "undefined") //true
变量定义后为赋值即为 undefined
var content = null;if(typeof content === null) //true
trim() 方法用于删除字符串的头尾空白符,空白符包括:空格、制表符 tab、换行符等其他空白符等。
var content = " "; //一列空格if(typeof content.trim() === "") //true
综上,上面的判断顺序不能变换,写法如下:
if(typeof content === "undefined" || content === null || content.trim() === "") { this.$message({ showClose: true, message: '空字符串', type: 'error' });}
参考文章:
转载地址:http://rzgvz.baihongyu.com/