This article is written by Gentle_Zhou in TypeScript The Difference between Strings and Strings.
background
Unlike the JavaScript language, TypeScript uses static typing, such as specifying the data types that variables can hold. Type comments can only be used in TS files, as shown in the following figure:
TypeScript is a superset of JavaScript. TypeScript needs to be compiled (syntactically transformed) to generate JavaScript before it can be executed by the browser. It also distinguishes between string and string data types. In general, string represents a primitive type and string represents an object.
The original string
JavaScript supports six native types (number) in the ES6 standard, and string is one of them.
A native string is a value that contains no attributes (i.e., no properties), including literally-untyped, literally-defined string, literally-defined string, and some strings returned from string calls.
The typeof the above three variables (typeof()) is string.
Object String
An object is an accumulation of different properties, and an object can call many corresponding methods. let msg3: String = new String(‘Hello world! ‘);
This variable is of type object: console.log(typeof(msg3)); // object
The String object supports the following methods:
Code comparison
We explore and compare the types of the following four variables:
let msg: string = 'Hello world! '; let msg2: String = 'Hello world! '; let msg22 = 'Hello world! '; // let msg3: String = new String('Hello world! '); console.log(typeof(msg)); //string console.log(typeof(msg2)); //string console.log(typeof(msg22)); //string console.log(typeof(msg3)); //object console.log(msg === msg2); //true console.log(msg === msg3); //false console.log(msg2 === msg3); //falseCopy the code
Why do WE need strings
First, when we create a new String using the keyword new, TS creates a new object. That is, we create two new strings that point to different memory even though they have the same content.
Here are two chestnuts:
1. When a1 and b1 are used to represent two variables with the same value, they are identical; When two objects are created with new, they are different, even if they have the same value (false, true) :
The eval() function evaluates an expression. If we assign eval() directly to a string that contains the evaluated string, it will return the evaluated value; If we assign eval() to String, since it is not a native type, it will only return String (the following image prints 27, :”8 + 20″, 28) :
Second, because strings can have properties. We can use String to keep an extra value in the property. Even though this usage is uncommon, it is still a feature of TS:
var prim = 'hello HW'; var obj = new String('hello HW Cloud'); prim.property = 'PaaS'; // Invalid obj.property = 'PaaS'; // Valid console.log(obj.property); // Output is PaaSCopy the code
Summary of the differences between the two
String native type | String object |
---|---|
Widely used | It’s rarely used |
Only values are retained | The ability to retain attributes in addition to values |
Values are immutable and therefore thread-safe | Strings are mutable |
There is no way | String objects have various methods |
You cannot create two independent strings that have the same literal value | You can usenew Create two objects |
Is a native data type | Wrap native data types to create an object |
The value passed is a copy of the native data itself | The value passed is a reference to the actual data |
When the eval() function is used, it is treated directly as source code | When the eval() function is used, it is converted to a string |
Refer to the link
- www.geeksforgeeks.org/what-is-the…
- www.geeksforgeeks.org/variables-d…
- www.tutorialspoint.com/typescript/…
Click to follow, the first time to learn about Huawei cloud fresh technology ~