1. 程式人生 > >js 判斷物件是否是字串

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]"