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

1

Reading an article I find the following excerpt:

  

You can call any of the methods of the string object in a   string literal - 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 am wrong, but from what I understand when I define a string literal it converts the same to a String temporary object, my doubt is the following, there is difference between doing:

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

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

    
asked by anonymous 23.08.2018 / 02:34

2 answers

2

JavaScript does not make much difference between a string value and a string object. Both allow you to use the same methods in your content, so you generally do not need 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 much like a full string object.

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

Objects strings defined with the new String("valorString") constructor are powerful objects compared to simple variables, which are given string values. You certainly do not need to create this type of object for each string in your scripts, but they are very handy if you find that the strings in the variables are wrong. This happens occasionally while attempting to preserve string information as script variables in other frames or windows. Using the string object constructor, you can have relative certainty that the string value will be available in the far frame when it is needed.

Another byproduct 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 a method that becomes part of every new object created after prototype items are added. For strings, for example, you might want to define a new method to convert a string to a new HTML source tag type, not yet defined by the JavaScript string object.

The following shows 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>")
    
23.08.2018 / 03:43
2

When you declare strings as in your example:

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

It has no difference whatsoever, the two forms are declaring primitive strings.

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

var palavra = new String('Palavra');

You should 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 a String object from a primitive string, as you yourself quote.

Even though they are almost the same in practice, the 2 still have some differences, among them, the type impression:

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

and the result of a mathematical operation in 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 valueOf method:

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

Credits to Mozilla official strings documentation .

    
23.08.2018 / 03:14