Javascript 如何检测变量是否为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4891937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:37:49  来源:igfitidea点击:

how to detect if variable is a string

javascriptjquery

提问by Webnet

How can I detect if a variable is a string?

如何检测变量是否为字符串?

回答by user113716

This is the way specified in the ECMAScript spec to determine the internal [[Class]] property.

这是 ECMAScript 规范中指定的确定内部 [[Class]] 属性的方式。

if( Object.prototype.toString.call(myvar) == '[object String]' ) {
   // a string
}

From 8.6.2 Object Internal Properties and Methods:

8.6.2 对象内部属性和方法

The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp",and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).

[[Class]] 内部属性的值由本规范为每种内置对象定义。宿主对象的 [[Class]] 内部属性的值可以是任何字符串值,除了“Arguments”、“Array”、“Boolean”、“Date”、“Error”、“Function”、“JSON”之一、“数学”、“数字”、“对象”、“RegExp”“字符串”。[[Class]] 内部属性的值在内部用于区分不同种类的对象。请注意,除了通过 Object.prototype.toString(见 15.2.4.2)之外,本规范没有为程序提供任何访问该值的方法。



For an example of how this is useful, consider this example:

有关这如何有用的示例,请考虑以下示例:

var str = new String('some string');

alert( typeof str ); // "object"

alert( Object.prototype.toString.call(str) ); // "[object String]"

If you use typeof, you get "object".

如果你使用typeof,你会得到"object"

But if you use the method above, you get the correct result "[object String]".

但是如果你使用上面的方法,你会得到正确的结果"[object String]"

回答by Daniel DiPaolo

You can use typeofto do it, but for a lot of things this is bad design.

你可以用typeof它来做,但对于很多事情来说,这是一个糟糕的设计。

if (typeof myVar == "string") {
    alert("I'm a string!");
}

回答by simshaun

Use typeof.

使用类型。

if (typeof foo == 'string')

回答by The Mask

var str = new String("some string");
if(str.constructor.name === "String") 
     {
        alert("I'm a string!");
      }

or simply:

或者干脆:

if(str.constructor === String) 
     {
        alert("I'm a string!");
      }

回答by user1607706

I don't think we need to treat new String('some string')as a string, because if we try:

我认为我们不需要将其new String('some string')视为字符串,因为如果我们尝试:

new String('abc')==='abc'

It will return false!

它会返回错误!

But if we try:

但是如果我们尝试:

String('abc')==='abc'

It will return true.

它会返回真。

回答by Cody

('str'.constructor === String) && alert('its a string!');

回答by Adil Mehmood

Use alert(typeof "hello");alert(typeof 123);

alert(typeof "hello");alert(typeof 123);

Ref.: here.

参考:这里

回答by Radek Benkel

typeof('yourstring');// returns string

typeof('yourstring');// returns string