检查对象是不是是Javascript中的字符串

Posted

技术标签:

【中文标题】检查对象是不是是Javascript中的字符串【英文标题】:check if an object is string in Javascript检查对象是否是Javascript中的字符串 【发布时间】:2014-09-10 17:10:07 【问题描述】:

我正在关注一个建议检查对象是否为字符串且不为空的教程,如下所示:

var s = "text here";
if ( s && s.charAt && s.charAt(0))

据说如果s是字符串,那么它有一个charAt方法,然后最后一个组件会检查字符串是否为空。

我尝试使用其他可用方法来测试它,例如(typeofinstanceof)使用一些 SO questions 和 here 和 here too !!

所以我决定在 Js Bin 中测试它:jsbin code here 如下:

var string1 = "text here";
var string2 = "";


alert("string1  is " + typeof string1);
alert("string2  is " + typeof string2);


//part1- this will succeed and show it is string
if(string1 && string1.charAt)
  alert( "part1- string1 is string");
else
  alert("part1- string1 is not string ");



//part2- this will show that it is not string
if(string2 && string2.charAt )
  alert( "part2- string2 is string");
else
  alert("part2- string2 is not string ");




//part3 a - this also fails !!
if(string2 instanceof String)  
  alert("part3a- string2 is really a string");
else
  alert("part3a- failed instanceof check !!");


//part3 b- this also fails !!
//i tested to write the String with small 's' => string
// but then no alert will excute !!
if(string2 instanceof string)  
  alert("part3b- string2 is really a string");
else
  alert("part3b- failed instanceof check !!");

现在我的问题是:

1-为什么使用string2.charAt字符串为空时检查字符串失败???

2- 为什么instanceof 检查失败??

【问题讨论】:

if(string2.charAt) 只检查是否定义了方法,空字符串仍然是字符串,因此将返回 true @charlietfl 请参考 adeneo 的回答,他说“简单的字符串不是对象,它是主要数据类型,并且没有原型,而不是使用新字符串创建的字符串对象。 " 因此,如果检查 charAt 函数是否存在,定义为文字的空字符串将不会返回 true @stackunderflow 如果使用提供的代码,它会。 (因为"".charAt(0) -> "" -> false-y不起作用的东西是"" instanceof String && ..,因为它是一个string值,而不是一个String对象。 @stackunderflow 按照你的逻辑你永远做不到'somestring'.charAt(n) 【参考方案1】:

字符串值不是字符串对象(这就是instanceof失败的原因)2

要使用“类型检查”覆盖这两种情况,它会是typeof x === "string" || x instanceof String;第一个只匹配strings,后者匹配Strings

本教程假定 [仅] 字符串对象 - 或提升的字符串值1 - 具有 charAt 方法,因此使用 "duck-typing"。如果该方法确实存在,则调用它。如果charAt 使用越界,则返回一个空字符串"",它是一个false-y 值。

教程代码也可以接受字符串“\0”,而s && s.length 不会——但它也可以在数组(或 jQuery 对象等)上“工作”。就个人而言,我信任调用者提供允许的值/类型,并尽可能少地使用“类型检查”或特殊大小写。


1 对于字符串、数字和布尔值的原始值,对应的对象类型分别是字符串、数字和布尔值。当x.property 用于这些原始值之一时,效果是ToObject(x).property - 因此是“促销”。这在ES5: 9.9 - ToObject 中进行了讨论。

null 或 undefined 值都没有对应的对象(或方法)。函数已经是对象,但具有历史上不同且有用的typeof 结果。

2 请参阅ES5: 8 - Types 了解不同类型的值。例如,String 类型表示 string 值。

【讨论】:

【参考方案2】:

1-为什么使用string2.charAt当字符串为空时检查字符串失败?

以下表达式的计算结果为 false,因为第一个条件失败:

var string2 = "";
if (string2 && string2.charAt)  console.log("doesn't output"); 

第二行基本上相当于:

if (false && true)  console.log("doesn't output"); 

例如:

if (string2)  console.log("this doesn't output since string2 == false"); 
if (string2.charAt)  console.log('this outputs'); 

2- 为什么instanceof检查失败?

这会失败,因为在 javascript 中,字符串可以是文字或对象。例如:

var myString = new String("asdf");
myString instanceof String; // true

但是:

var myLiteralString = "asdf";
myLiteralString instanceof String; // false

你可以通过检查类型和instanceof来可靠地判断它是否是一个字符串:

str instanceof String || typeof str === "string";

【讨论】:

你错过了一些东西。我的问题是没有把参数归零。它实际上没有括号。 if(string1 && string1.charAt)

以上是关于检查对象是不是是Javascript中的字符串的主要内容,如果未能解决你的问题,请参考以下文章

如何检查一个对象是不是至少包含一个键,其值包含 JavaScript 中的子字符串?

JavaScript/jQuery - 如何检查字符串是不是包含特定单词

检查元素是不是是javascript中的对象[重复]

如何检查获取的响应是不是是javascript中的json对象

如何检查 Javascript 中是不是存在字符串/数组/对象? [复制]

检查Javascript中对象数组中的键是不是?