JavaScript basic learning series 18: original string

Using template literals, you can also directly obtain the original template literal content (such as newline characters or Unicode characters), rather than the converted character representation. For this purpose, the default String.raw tag function can be used:

  // 换行符示例
console.log(`first line\nsecond line`); // first line
// second line
console.log(String.raw`first line\nsecond line`); // "first line\nsecond line"
// 对实际的换行符来说是不行的
// 它们不会被转换成转义序列的形式 console.log(`first line

In addition, you can also get the original content of each string through the first parameter of the label function, that is, the .raw property of the string array:

    function printRaw(strings) {
    
    
      console.log('Actual characters:');
      for (const string of strings) {
    
    
        console.log(string);
      }
      console.log('Escaped characters;');
      for (const rawString of strings.raw) {
    
    
        console.log(rawString);
      }
}
printRaw`\u00A9${
      
       'and' }\n`; // Actual characters:
// ©
//(换行符)
    // Escaped characters:
    // \u00A9
    // \n

1. Symbol type:

Symbol (symbol) is a new data type in ECMAScript 6. Symbols are primitive values, and symbol instances are unique and immutable. The purpose of symbols is to ensure that object properties use unique identifiers and there is no danger of property conflicts.

Although it sounds similar to private properties, symbols are not added to provide private property behavior (especially because the Object API provides methods to make it easier to discover symbol properties). Instead, symbols are used to create unique tokens that can be used as non-string object properties.

Symbols need to be initialized using the Symbol() function. Because symbols themselves are primitive types, the typeof operator returns symbol for symbols.

  let sym = Symbol();
    console.log(typeof sym); // symbol

When calling the Symbol() function, you can also pass in a string parameter as a description of the symbol. You can use this string to debug the code in the future. However, this string argument has absolutely nothing to do with symbol definition or identification:

let genericSymbol = Symbol();
    let otherGenericSymbol = Symbol();
    let fooSymbol = Symbol('foo');
    let otherFooSymbol = Symbol('foo');
console.log(genericSymbol == otherGenericSymbol);  // false

Symbols have no literal syntax, which is the key to their usefulness. According to the specification, as long as you create a Symbol() instance and use it as a new property of the object, you can guarantee that it will not overwrite existing object properties, whether they are symbol properties or string properties.

let genericSymbol = Symbol();
console.log(genericSymbol);  // Symbol()
let fooSymbol = Symbol('foo');
console.log(fooSymbol);      // Symbol(foo);

Most importantly, the Symbol() function cannot be used as a constructor with the new keyword. This is done to avoid creating symbolic wrapper objects, like using Boolean, String or Number, which all support constructors and can be used to initialize wrapper objects containing primitive values:

Guess you like

Origin blog.csdn.net/wanmeijuhao/article/details/135442502