1、不少JS针对字符串的判断
JS验证字符串功能
2、判断是不是字符串
// 判断是不是字符串
typeof(str)=='string'
// 利用js原生方法
Object.prototype.toString // ƒ toString() { [native code] }
Object.prototype.toString.call(str)=="[object String]"
(1)在Object.prototype这个this(上下文环境)中执行toString原生函数,会把里边的环境变量类型打印出来。
Object.prototype.toString() -->执行结果-->"[object Object]"
(2)如果我们改变this(上下文环境),就能打印出当前环境变量类型,根据这个类型来判断。
Object.prototype.toString.call(str) -->执行结果-->"[object String]"
3、js封装
// 工具,注意无参数
function tool() {
console.log('i am a tool function.');
}
// 有参数
// 可以是多个参数,用逗号隔开
var getImageurl = function(imageurl){
return URl + imageurl;
}
module.exports = {
tool: tool
}
// 使用时,进行引用
const util = require('../../common/utils/util');
// 调用
util.tool();
关于Util文件
util.js
网友评论