js判斷某變量是否是字符串

var str = 'asdf';
var str1 = new String('asdf');
typeof str;//"string"
typeof str1;//"object"
Object.prototype.toString.cal(str); //"[object String]"
Object.prototype.toString.call(str1); //"[object String]"
綜上所述,判斷是否爲字符串使用下邊的最保險
Object.prototype.toString.call(str) === "[object String]"
相關文章
相關標籤/搜索