When should I use a string object and a literal string?

Asked

Viewed 301 times

2

Reading an article I find the following excerpt:

You can call any of the methods of the string object in a literal string - Javascript automatically converts the literal string for a temporary string object, calls the method, then discards the temporary string object. You can also use the property String.length with a literal string:

console.log("John's cat".length) 
// Irá exibir a quantidade de caracteres na string incluindo o espaço em branco. 
// Nesse caso, 10 caracteres.

Correct me if I’m wrong, but from what I understand when I define a string literal it converts the same to an object String temporary, my doubt is the following, there is difference between making:

var palavra = 'Palavra'; // String literal
var palavra = String('Palavra'); // String object

When I should wear a string literal or a objeto String?

2 answers

3

When you declare strings as in your example:

var palavra = 'Palavra';
var palavra = String('Palavra');

There is no difference, the 2 forms are declaring primitive strings.

I believe the article you read from was talking about the statement using the keyword new in string creation, which actually declares an object String:

var palavra = new String('Palavra');

You must not declare object String unless you really need it. Javascript already does this conversion automatically, so it is possible to access methods and properties of an object String from a primitive string, as you yourself quote.

Even being almost the same thing in practice, the 2 still have some differences, between them, the impression of type:

console.log(typeof "string primitiva") // string
console.log(typeof new String("objeto string")) // object

and the result of a mathematical operation on a eval:

console.log(eval("1 + 1")) // 2
console.log(eval(new String("1 + 1"))) // 1 + 1

But still, having an object String, you can easily convert it back to a primitive string using the method valueOf:

console.log(typeof new String("string").valueOf()) // string

Credits to official Mozilla documentation on strings.

2


Javascript doesn’t make much difference between a string value and a string object. Both allow you to use the same methods in your content, so in general you don’t have to create a string object every time you want to assign a string value to a variable. A simple assignment operation (var palavra = 'Palavra';) is all you need to create a string value that behaves, on the surface, very similar to a full string object.

The difference appears exactly when you want to explore the "object side" of a genuine string object.

Strings objects defined with the constructor new String("valorString") are powerful objects compared to simple variables, which receive string values. You certainly don’t need to create this type of object for each string in your scripts, but they are very practical if you find that the strings in the variables are wrong. This happens occasionally while trying to preserve string information as script variables in other frames or windows. Using the string object constructor, you can be relatively sure that the string value will be available in the far frame when needed.

Another by-product of true string objects is that you can position prototype properties and methods to all string objects in the document. A prototype is a property or method that becomes part of each new object created after the prototype items are added. For strings, for example, you may want to define a new method to convert a string into a new type of HTML font tag, not yet defined by the Javascript string object.

The following is shown how to create and use this type of prototype.

<HTML>
<HEAD>
<TITLE>String Object Prototype</TITLE>
<SCRIPT LANGUAGE="JavaScript1.1">
function makeItHot() {
    return "<span style='color:red'>" + this.toString() + "</span>"
}
String.prototype.hot = makeItHot
</SCRIPT>
<BODY>
<SCRIPT LANGUAGE="JavaScript1.1">
document.write("<H1>This site is on " + "FIRE".hot() + "!!</H1>")
</SCRIPT>
</BODY>
</HTML>

The result is as follows:

function makeItHot() {
	return "<span style='color:red'>" + this.toString() + "</span>"
}
String.prototype.hot = makeItHot


document.write("<H1>This site is on " + "FIRE".hot() + "!!</H1>")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.