preface

This article is ES6 related knowledge, although the knowledge is a little old, I as a beginner have to learn ah! Everyone together!! Come on!

Literal reinforcement

literal

Literal (12345, true, false, null, [1, 2, 3, 4]…)

Original literals enhanced

Safer literal (0b1111101)

MDN numbers and dates ES5 and ES6 are compared

  1. Octal (0O767) 0777 (ES5)



    0o777 (ES6)



    0888 in octal

    Octal added in ES60oIf written in decimal, it will be shown in the figure above. Solid is safer

  2. Binary is 0b to make it more secure (0b1111101)

Unicode support for strings

Ruan Yifeng wrote Unicode and JavaScript details

  • String.fromCodePoint
  • String.prototype.codePointAt

  • Regular expression literals add Unicode support (U flag)
  • Y flags are added to regular expressions to support sticky matching

Symbol and iterator

There are eight common data types in JavaScript, namely, Undefined, Null, Boolean, Number, String, Object, Symbol and BigInt.

Symbol creates a unique value

Object attribute enhancement

  • Attribute definitions support phrasal methodsobj = { x, y }
  • Attribute names support expressionsobj = {["baz" + quux() ]: 42}
  • Add a __proto__ attribute, but use is not recommended

The iteration

Traversal is an iteration of access: Vue2 => Vue3,LOL version 1.1=>2.2=>… What’s the difference between iterating and iterating? Traversal is a finite set of known data, iteration is unknown, and we never know how many versions of LOL will be upgraded to \



As shown in the figure above, something returns to the next version every time you dial it. This is iteration

nextWhich means the next version of the portal,doneI don’t have the next version



Changes in the



Combined with the done \



The iteratorYou can ask me every time if I have the next version or if I have the next version, and I’ll give you a done if I haven’t, and you ask me if I’ve already done it. \

conclusion

So an iterator is a thing, it has anextAPI, and this API will return onevalueAnd adone“Value” means its value, “done” means whether it’s finished or not, and if it’s not finished it will give me a new value every time, and if it’s finished it will give us a “done” yestrue

Generator & for… of

First, rewrite the above code as

function* Publishers () {
  var version = 0
  while(true){
    version += 1
    yield version
  }
}
Copy the code

Run the

function* Publishers () {
// The following code will only be run once
  var version = 0
  while(true){
    version += 1
    yield version // There is a change at yield}}Copy the code

If you drop the yield, the version will not yield. If you drop the yield, the version will yield. After next, the first yield will yield a 1, the second yield a 2, and the third yield a 3. . And then I quit and I’m going to stop there until I call next, and I’m going to keep going

Let’s take a look at this code



The above code runs toversion += 1When it is not running (it stops at yield), it is waiting for it to gonext.nextAnd then it’s going to exit the 1, and it’s going to go on to the next one, and this time fromtrueTo begin with, thisversionTheta is equal to 2, and at that point it’s going to sit there and wait for me to continueyieldWhen I runnextThe iterator will loop once as long as.next. The iterator will loop once as long as.nextreturn, just put the value of the iteration each timeyieldJust come out.

So what’s the point of it?

The back is done

Summary of iterators and generators

Links to MDN 🔗

  1. The iteratorI have an object it has an objectnextMethod, this onenextMethod, which returns one when called oncevalueOne call will return onevalue
  2. The generatorIs a syntactic sugar generated by iterators. If you use generators, you just need to add one after the function*Sign, and then add one to the preceding value each timeyield“, it will automatically generate an iterator for me.
  3. For of is the syntactic sugar for iterator access

iterable

MDN给的解释👇

What does that mean? Let’s see if the array iterates



It means 👇



thenlet item of arrayWhat does that mean? So this is going to go back to the ergodic explanation again



If I add a property to array



So I’m wondering if you should type it when you’re going through itxThe value of the 🤔 🤔 🤔 \

  • Shouldn’t?
  • Why not? In other words, its traversal is a deliberate attempt to look only at numbers and not at other keys 😳
  • There’s a length that we don’t see, so are we still going through the whole thing, or are we still going through it? I didn’t read all the keys

Let’s simulate the array 👇



Should I print this if I’m going to iterate over this objectx?????

  • The?

So what’s the basis for that? If it’s a number I have to look at its numeric indices, if it’s not an array I have to look at all its indices, right? Is that so ???? 🤔🤔🤔 to do an experiment what if the object and array are identical? So let’s type out array’s subscript



It has no numeric index, which means that the object is no different from the array.

Now turn this object into an array 👇



Now they are exactly the same, which means that objects are no different from arrays.

So if they are identical, why are their traversal different??

Going back to the basic data types of JS,An array is a special kind of objectSo why does it feel different when traversing? ?

It’s what we think of as the term array, semantically what we think of as an array is the first term, the second term, the third term, the fourth term… We think of objects as being manykey,valueSimilarly, if you think of it as an iterator, then thisIterative objectThere will be onenextMethod, theniterable? Some objects are iterable, such as the array 👇



All of the above can be iterated over, but only arrays can be iterated over, not objects, so why can’t objects be iterated over (for now, not if you want to)?? 🤔

Something is iterable if it can be used by for of

Before the real traversal, regardless of any object can traverse 👇



The current iteration 👇\

The difference between arrays and objects

Arrays and objects are the same thing, just different objects, arrays are not ordered, we just think they are ordered so what’s the essential difference? Their __protype__, Array.prototype has the pop push attribute, and Objec. Prototype has the pop push attribute The toString properties.

Back to iterator

That is to say, as long as the object can be iterated, only objects that meet certain characteristics can be iterated, and the array has its own iteration function

The item you returned above has nothing to do with what this object has, so let’s make it have something to do with

So why can arrays iterate, but not objects? \

  • Because arrays know how to access them in order, object doesn’t know how to access them, whether to give them to A first, b first, or C first.

Symbol was originally introduced to implement an iterator to implement an iterator object, which is a unique value

JS realized that to implement an iterator I had to give a syntax sugar, so it invented generators

// Generator syntax in MDN
function* fibonacci() {
  var fn1 = 0;
  var fn2 = 1;
  while (true) {
    var current = fn1;
    fn1 = fn2;
    fn2 = current + fn1;
    var reset = yield current;
    if (reset) {
        fn1 = 0;
        fn2 = 1; }}}// One * one yield
Copy the code

We then wanted some objects to iterate by default, so we introduced a special attribute name symbol.iterator

var myIterable = {
  *[Symbol.iterator]() {
    yield 1;
    yield 2;
    yield 3; }}Copy the code