This is the 16th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.

preface

ECMAScript 6.0, or ES6 for short, is a new generation of JavaScript standard. It was released in June 2015 and is officially known as ECMAScript 2015 Standard. General, generally refers to the standard after 5.1 version, covering ES2015, ES2016, ES2017, ES2018, ES2019, ES2020, ES2021 and so on

Let’s look at two static methods for ES6 strings:

  • String.fromCodePoint
  • String.raw

String.fromCodePoint

Returns the corresponding character based on the entered Unicode code point.

String.fromCodePoint(0x61.0x20034) / / 'a 𠀴'
Copy the code

ES5 actually has a very similar method, string.fromCharcode, but it does not recognize code points larger than 0xFFFF (decimal: 65535).

String.fromCharCode(0x61.0x20034) // 'a4'
Copy the code

If the String. FromCharCode identifier is greater than 0xFFFFF, the high order is removed. So the square above is equal to:

String.fromCharCode(0x61.0x0034) // 'a4'
String.fromCharCode(0x61.0x20034) = =String.fromCharCode(0x61.0x0034) // true
Copy the code

There is, of course, a more concise initialization method:

var text =  "\u0061\u{20034}" 
console.log(text);  / / 'a 𠀴'
Copy the code

For characters with code points greater than 0xFFFF, the format is \u{XXXXX}, with large parentheses. Anything less than 0xFFFF is optional.

'\u{20034}'  / / '𠀴'
'\u{0061}'   // 'a'
'\u0061'     // 'a'
Copy the code

With String. FromCharCode corresponding way, is a String. The prototype. CodePointAt, one is a static method, is a prototype method, namely instance methods.

var text = String.fromCodePoint(0x61.0x20034); text.length/ / 3
text[0] // 'a'
text[1] // '\uD840'
text[2] // '\uDC34'

text.codePointAt(0);  // 'a' 
text.codePointAt(1);  / / '𠀴'
Copy the code

For text[0], the output for text[1] looks like this, see string traversal

String.raw

Retrieving the original string of a template string, for example, placeholders (such as ${foo}) are treated as other strings it represents, while escape characters (such as \n) are not.

There are two kinds of syntax:

String.raw(callSite, … substitutions) String.raw`templateString`

Let’s start with a simple mdNString. raw example:

`Hi\nThe ${2+3}! `            // 'Hi\n5! '
String.raw`Hi\nThe ${2+3}! `  // 'Hi\n5! '

String.raw `Hi\u000A! `   // 'Hi\u000A! '
`Hi\u000A! `              // 'Hi\n! '
Copy the code

As you can see, the ${} calculation is executed, and the rest of the output is left as is.

Here’s another syntax:

String.raw(callSite, … substitutions)

  • callSite

    A “call point object” for a template string. similar{ raw: ['foo', 'bar', 'baz'] }.
  • . substitutions

    Any optional argument that represents the value of any interpolation expression.

Look at an example:

String.raw({
  raw: ['foo'.'bar'.'baz']},2 + 3.'Java' + 'Script');
// equivalent to 'foo${2 + 3}bar${'Java' + 'Script'}baz'
Copy the code

Raw goes one, then, substitutions goes one. It’s that simple.

If, substitutions is not long enough, what would you use it for, of course, by simply skipping it and moving on to the next cycle?

String.raw({
  raw: ['foo'.'bar'.'baz']})// 'foobarbaz'
Copy the code

Conversely, what if raw isn’t long enough?

String.raw({
  raw: ['foo']},2 + 3.'Java' + 'Script')  // 'foo'
Copy the code

As a result, stop going down, a single raw goes over!!

The special thing here are strings:

String.raw({ raw: 'ABCD' }, 0.1.2) // 'A0B1C2D'
Copy the code

It’s actually pretty easy to understand, right? Take the subscript!

We can also change the class array, so can the arguments.

String.raw({ raw: { 
    length: 2 ,
    0: "A".1: "B"
}}, 0) // 'A0B'
Copy the code

Let’s try to remove the length of the class array.

String.raw({ raw: { 
    0: "A".1: "B"
}}, 0)  / /"
Copy the code

The question remains, if an object implements an iterator, can it be properly parsed?

summary

Did you harvest anything today?