为什么有两种JavaScript字符串? [英] Why are there two kinds of JavaScript strings?

查看:127
本文介绍了为什么有两种JavaScript字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个只是刺伤了我。我不知道所有浏览器是否都是这种情况(我没有任何其他有能力的浏览器进行测试),但至少Firefox有两种字符串对象。

This one just stabbed me hard. I don't know if it's the case with all browsers (I don't have any other competent browser to test with), but at least Firefox has two kind of string objects.

打开Firebugs控制台并尝试以下操作:

Open up the Firebugs console and try the following:

>>> "a"
"a"
>>> new String("a")
String { 0="a"}

当你可以直观地观察,Firefox以不同方式处理新字符串(a)a。否则,两种字符串似乎表现相同。例如,有证据表明两者都使用相同的原型对象:

As you can visually observe, Firefox treats new String("a") and "a" differently. Otherwise however, both kinds of strings seem to behave the same. There is, for instance, evidence that both use the same prototype object:

>>> String.prototype.log = function() { console.log("Logged string: " + this); }
function()
>>> "hello world".log()
Logged string: hello world
>>> new String("hello world").log()
Logged string: hello world

显然,两者都是一样的。也就是说,直到您要求输入类型。

So apparently, both are the same. That is, until you ask for the type.

>>> typeof("a")
"string"
>>> typeof(new String("a"))
"object"

我们还可以注意到当这个是一个字符串时,它始终是对象形式:

We can also notice that when this is a string, it's always the object form:

>>> var identity = function() { return this }
>>> identity.call("a")
String { 0="a"}
>>> identity.call(new String("a"))
String { 0="a"}

进一步说,我们可以看到非对象字符串表示不支持任何其他属性,但对象字符串确实:

Going a bit further, we can see that the non-object string representation doesn't support any additional properties, but the object string does:

>>> var a = "a"
>>> var b = new String("b")
>>> a.bar = 4
4
>>> b.bar = 4
4
>>> a.bar
undefined
>>> b.bar
4

此外,有趣的事实!您可以使用 toString()函数将字符串对象转换为非对象字符串:

Also, fun fact! You can turn a string object into a non-object string by using the toString() function:

>>> new String("foo").toString()
"foo"

从未想过可以用来调用 String.toString()!无论如何。

Never thought it could be useful to call String.toString()! Anyways.

所以这些实验都提出了一个问题:为什么JavaScript中有两种字符串?

So all these experiments beg the question: why are there two kinds of strings in JavaScript?

评论显示每种原始JavaScript类型(包括数字和bool)也是如此。

Comments show this is also the case for every primitive JavaScript type (numbers and bools included).

推荐答案

Javascript中有两种类型的字符串 - 文字字符串和String对象。他们的行为有点不同。两者之间的主要区别是您可以向String对象添加其他方法和属性。例如:

There are two types of strings in Javascript -- literal strings and String objects. They do behave a little differently. The main difference between the two is that you can add additional methods and properties to a String object. For instance:

var strObj = new String("object mode");
strObj.string_mode = "object"
strObj.get_string_mode = function() { return this.string_mode; }

// this converts it from an Object to a primitive string:
str = strObj.toString();

字符串文字只是暂时转换为String对象以执行任何核心方法。

A string literal is just temporarily cast to a String object to perform any of the core methods.

同样的概念也适用于其他数据类型。 以下是有关原始数据类型和对象的更多信息

The same kinds of concepts apply to other data types, too. Here's more on primitive data types and objects.

编辑

如评论中所述,字符串文字不是原始字符串,而是文字常量,其类型为a内置原语[string] value,引用此来源

As noted in the comments, string literals are not primitive strings, rather a "literal constant whose type is a built-in primitive [string] value", citing this source.

这篇关于为什么有两种JavaScript字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆