简体   繁体   English

JavaScript和String作为原始值

[英]JavaScript and String as primitive value

In JavaScript a String is a primitive value. 在JavaScript中,String是原始值。 But is also a String object... A primitive value is a value put directly into a variable. 但也是一个String对象......原始值是直接放入变量的值。

So my question is: 所以我的问题是:

var d = "foo";

does d contain directly foo or a reference to a string object like other languages? d直接包含foo或对其他语言的字符串对象的引用?

Thanks. 谢谢。

If I understand it correctly, d will contain the string literal "foo", and not a reference to an object. 如果我理解正确, d将包含字符串文字“foo”,而不是对象的引用。 However, the JavaScript engine will effectively cast the literal to an instance of String when necessary, which is why you can call methods of String.prototype on string literals: 但是,JavaScript引擎会在必要时将文字有效地转换为String的实例,这就是为什么你可以在字符串文字上调用String.prototype方法:

"some string".toUpperCase(); //Method of String.prototype

The following snippet from MDN may help to explain it further (emphasis added): 来自MDN的以下片段可能有助于进一步解释(重点补充):

String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (ie, without using the new keyword) are primitive strings . 字符串文字 (用双引号或单引号表示)和从非构造函数上下文中的String调用返回的字符串(即,不使用new关键字) 是原始字符串 JavaScript automatically converts primitives and String objects, so that it's possible to use String object methods for primitive strings. JavaScript自动转换基元和String对象,因此可以对原始字符串使用String对象方法。 In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup. 在要在原始字符串上调用方法或发生属性查找的上下文中,JavaScript将自动包装字符串原语并调用方法或执行属性查找。

This is all explained in detail in the specification , but it's not exactly easy reading. 这在说明书中都有详细解释,但阅读起来并不容易。 I asked a related question recently (about why it is possible to do the above), so it might be worth reading the (very) detailed answer. 我最近问了一个相关的问题 (关于为什么可以做到这一点),所以可能值得阅读(非常)详细的答案。

if you define 如果你定义

var d = "foo";

than d contains directly foo but, if you define d直接包含foo但是,如果你定义

var S = new String("foo");

then S is an Object 然后S是一个Object

Example: 例:

var s1 = "1";
var s2 = "1";
s1 == s2 -> true
var S1 = new String("2");
var S2 = new String("2");
S1 == S2 -> false

I think that every variable in Javascript actually represents an Object. 我认为Javascript中的每个变量实际上都代表一个Object。 Even a function is an Object. 甚至函数也是一个Object。

I found two useful articles detailing this, located here and here . 我找到了两篇有用的文章,详细介绍了这里这里 Seems like primitive types in JavaScript are passed by VALUE (ie when you pass if to a function it gets "sandboxed" within the function and the original variable's value won't change), while reference types are passed, you guessed it, by REFERENCE and passing it through to a function will change the original variable. 看起来像JavaScript中的原始类型是由VALUE传递的(即当你将函数传递给函数时它会在函数中“沙盒化”并且原始变量的值不会改变),而参考类型被传递,你猜对了,参考文献并将其传递给函数将更改原始变量。

Primitive types in JavaScript are text (string), numeric (float / int), boolean and NULL (and the dreaded "undefined" type). JavaScript中的原始类型是文本(字符串),数字(浮点数/整数),布尔值和NULL(以及可怕的“未定义”类型)。 Any custom objects, functions or standard arrays are considered reference types. 任何自定义对象,函数或标准数组都被视为引用类型。 I haven't researched the Date type though, but I'm sure it will fall into the primitive types. 我没有研究过Date类型,但我确信它会属于原始类型。

Found this page about javascript variables, seems that: 找到这个关于javascript变量的页面,似乎是:

Primitive type for javascript are booleans, numbers and text. javascript的原始类型是布尔值,数字和文本。

I believe there are no primitives in Javascript, in the Java sense at least - everything is an object of some kind. 我相信Javascript中没有原语,至少在Java意义上 - 一切都是某种对象。 So yes it is a reference to an object - if you extend the String object, d would have that extension. 所以是的,它是对象的引用 - 如果你扩展String对象, d将具有该扩展名。

If you mean primitives as in those types provided by the language, you've got a few, boolean, numbers, strings and dates are all defined by the language. 如果您指的是语言提供的那些类型中的原语,那么您有一些布尔数字,字符串和日期都由语言定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM