js 判断对象是否是字符串

function isString(obj){ //判断对象是否是字符串
  return Object.prototype.toString.call(obj) === "[object String]";
}

验证:

var str1 = 'abc';

var str2 = new String('abc');


typeof str1; //"string"

typeof str2; //"object"


Object.prototype.toString.call(str1); //"[object String]"

Object.prototype.toString.call(str2); //"[object String]"

你可能感兴趣的:(JavaScript)