JavaScript中如何检测一个变量是一个String类型?请写出函数实现

1.function a(obj){

return typeof(obj)=="string";

}

alert(a(123));

alert(a("abc"));

2.function b(obj){

  return obj.constructor === String

}

alert(b(123));

alert(b("abc"));

3.function type(data){

return Object.prototype.toString.call(data).slice(8,-1).toLowerCase();

}

alert(type(123));

alert(type("abc"));

猜你喜欢

转载自blog.csdn.net/weixin_41615439/article/details/84559033