简体   繁体   中英

JavaScript and String as primitive value

In JavaScript a String is a primitive value. But is also a String object... A primitive value is a value put directly into a variable.

So my question is:

var d = "foo";

does d contain directly foo or a reference to a string object like other languages?

Thanks.

If I understand it correctly, d will contain the string literal "foo", and not a reference to an object. 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:

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

The following snippet from MDN may help to explain it further (emphasis added):

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 . JavaScript automatically converts primitives and String objects, so that it's possible to use String object methods for primitive strings. 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.

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

var S = new String("foo");

then S is an 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. Even a function is an 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.

Primitive types in JavaScript are text (string), numeric (float / int), boolean and NULL (and the dreaded "undefined" type). 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.

Found this page about javascript variables, seems that:

Primitive type for javascript are booleans, numbers and text.

I believe there are no primitives in Javascript, in the Java sense at least - everything is an object of some kind. So yes it is a reference to an object - if you extend the String object, d would have that extension.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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