javascript字符串类型和字符串对象之间的区别? [英] Difference between the javascript String Type and String Object?

查看:361
本文介绍了javascript字符串类型和字符串对象之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搞乱ECMA-262标准( ECMAScript语言规范,第3版,如果它对此很重要 - 我没有发现第3版和第5版之间的任何区别类型/字符串对象)。

I've been messing around with the ECMA-262 standard (ECMAScript Language Specification, 3rd edition, if it matters for this - I have not found any difference between the 3rd and 5th edition on String Type / String Object).

有一件事让我困惑:字符串类型和字符串对象之间的区别。是的我知道 String Type 是一系列16位UTF-16单位而 String Object 是内置对象及其内部的区别 Class 属性设置为String,其内部 Value 属性设置为 String Type 的值。

There's one thing that baffles me: the difference between the String Type and the String Object. Yes I know the difference in the sense that the String Type is a sequence of 16-bit UTF-16 units and the String Object is a built-in object with its internal Class property set to "String" and its internal Value property set to a value of the String Type.

但是读取规范,字符串类型似乎没有公开任何方法;也就是说,它只是一个没有任何附加属性的值。拿这个代码,一切都与预期完全一样:

But reading the specification, the string type does not seem to expose any methods; that is, it's just a value without any additional properties. Take this code, everything is exactly as expected:

document.writeln(typeof "foo"); // 'string'
document.writeln(typeof new String("foo")); // 'object'

第一种类型是实际的字符串类型和第二个是对象类型(它是类 String 的对象,但其数据类型是对象)。但是,看看这个:

The first type is the actual String Type and the second is the Object Type (it's an object of class String, but its data type is object). However, looking at this:

"foo".charAt(0);

fooStrObj = new String("Foo");
fooStrObj.charAt(0);

它们似乎都暴露了相同的函数,但字符串类型上没有函数在ECMA-262标准中定义;它暴露的所有函数都来自String.prototype对象(我看不到 String Type 在ECMA中神奇地暴露String.prototype对象的所有属性和函数这一事实的引用 - 262标准)。类型 String Type 的值也会自动提升为 String Object ,其原始 String Type 值为其内部 property?

They both seem to expose the same functions, but there are no functions on the String Type defined in the ECMA-262 standard; all the functions it exposes are from the String.prototype object (and I can see no reference to the fact that the String Type magically exposes all the properties and functions of the String.prototype object in the ECMA-262 standard). So are the values of type String Type automatically promoted to a String Object with the original String Type value as its internal Value property?

如果对它们进行完全相同的处理(对于所有意图和目的而言),为什么有两种不同的方式来表示字符串

And if they are treated exactly the same (which for all intents and purposes they seem to be), why have two different ways to represent a String?

推荐答案

字符串是JS中的值类型,因此它们不能附加任何属性任何尝试访问它们的属性都是技术上执行JS [[ToObject]]转换(本质上是新的String)。

Strings are a value type in JS, so they can't have any properties attached to them, no prototype, etc. Any attempt to access a property on them is technically performing the JS [[ToObject]] conversion (in essence new String).

简单方法区分差异的是(在浏览器中)

Easy way of distinguishing the difference is (in a browser)

a = "foo"
a.b = "bar"
alert("a.b = " + a.b); //Undefined

A = new String("foo");
A.b = "bar";
alert("A.b = " + A.b); // bar

此外,

"foo" == new String("foo")

is是的,它只是真实的,因为==运算符的隐式类型转换

is true, it is only true due to the implicit type conversions of the == operator

"foo" === new String("foo")

将失败。

这篇关于javascript字符串类型和字符串对象之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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