typeof 'hello'; // 'string' typeof new String('hello'); // 'object'
//判断是否是String function isString(str){ return ((str instanceof String) || (typeof str).toLowerCase() == 'string'); } //测试案例 var s1 = "abc", s2 = new String("abc"); console.log(isString(s1)+'\n'); console.log(isString(s2));
var str = "abc";var str = new String("abc");
var a="abc"; console.log(typeof a=="string");会返回true。
var b=new String("abc"); console.log(typeof b=="string");结果是返回false。
var str1 = "haha" var str2 = new String() console.log(str1 instanceof String) console.log(str2 instanceof String) console.log(typeof str1); console.log(typeof str2);
var str1 = "haha" var str2 = new String() function isStr(str) { if (str instanceof String || (typeof str).toLowerCase() == "string" ){ return true } else { return false } } console.log( isStr(str1)); //true console.log( isStr(str2)); //true