字符串是 Javascript 中的原始类型还是对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7675127/
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
Is String a Primitive type or Object in Javascript?
提问by Imran
Is String a Primitive type or Object in Javascript? Source says Undefined, Null, Boolean, Number and String are all primitive types in Javascript. But it says String is an Object too. I'm confused. Can someone please explain?
字符串是 Javascript 中的原始类型还是对象?Source 说 Undefined、Null、Boolean、Number 和 String 都是 Javascript 中的原始类型。但它说 String 也是一个对象。我糊涂了。有人可以解释一下吗?
Thank you in advance ;-)
先感谢您 ;-)
回答by ducin
Actually the same answer applies to: strings, numbers, booleans. These types have their primitive and object version which are coercedin the application runtime, under the hood (without your knowledge).
实际上,同样的答案适用于:字符串、数字、布尔值。这些类型有它们的原始版本和对象版本,它们在应用程序运行时被强制执行,在幕后(在您不知情的情况下)。
Coercion
强迫
JavaScript is weakly typed. This means that whenever your code wants to perform an operation with invalid datatypes (such as add a string to a number), JavaScript will try to find a best match for this scenario.
JavaScript 是弱类型的。这意味着,无论何时您的代码想要使用无效数据类型执行操作(例如向数字添加字符串),JavaScript 都会尝试为这种情况找到最佳匹配。
This mechanism is also called, as mentioned above, coercion.
如上所述,这种机制也称为强制。
Primitives and Properties
原语和属性
You can find following code confusing:
您会发现以下代码令人困惑:
> "hello world".length
11
because "hello world"
is a string literal, i.e. a primitive. And we know that primitives don't have properties. All is right.
因为"hello world"
是一个字符串文字,即一个原语。我们知道基元没有属性。一切都对。
So how does that work? Coercion - the primitive is wrapped with an object (coerced) for just a tiny fraction of time, the property of the object is used and immediately the object gets disposed.
那么它是如何工作的呢?强制 - 原语被一个对象(强制)包裹了一小部分时间,使用对象的属性并立即处理该对象。
Coercion working both ways
强制双向工作
So primitives are casted with their object wrapping versions - but it also works the other way round as well. Consider following code:
所以基元是用它们的对象包装版本来铸造的——但它也可以反过来工作。考虑以下代码:
> String("hello ") + String("world")
"hello world"
> Number(2) + 3
5
The objects are down-casted to their primitive versions in order to accomplish the operation.
为了完成操作,对象被向下转换为它们的原始版本。
Read this brilliant explanationto learn more.
阅读这个精彩的解释以了解更多信息。
回答by Raynos
Both.
两个都。
There is a String object and there are string literals.
有一个 String 对象和字符串文字。
You can call any string method on a literal and you can call any string method on a string object.
您可以对文字调用任何字符串方法,也可以对字符串对象调用任何字符串方法。
The major difference is that a string object generates a new objectso new String("foo") !== new String("foo")
主要区别在于字符串对象会生成一个新对象,因此new String("foo") !== new String("foo")
That and a String object is type "object"
and not "string"
那和一个 String 对象是类型"object"
而不是"string"
How to check for both?
如何检查两者?
if(typeof(s) == "string" || s instanceof String){
//s is a string (literal or object)
}
Credits to @Triynko for the snippet in the comments
评论中的片段归功于@Triynko
回答by Quentin
JavaScript has both primitive and object Strings.
JavaScript 有原始字符串和对象字符串。
> var foo = "foo"
undefined
> var bar = new String("bar");
undefined
> foo
"foo"
> bar
String
> typeof foo
"string"
> typeof bar
"object"
回答by aziz punjani
var a = "string";
typeof a // yields "string"
var a = new String('string');
typeof a // yields "object"