The 2019-2-20

What is typeof ($)? || jQuery

  • Function?
  • Because we look at his source code

  • We can see thatJQuery is a function wrapped directlyIts prototype encapsulates the {} object

Vue use async, how to use, he ah implementation principle is?

  • To understand Async, we need to understand Generator
  • It is too long to talk about here, please check out Ruan yifeng’s es6 introduction to Generator syntax chapter
  • Ok, so let’s go back to async now let’s explain what an async function is? In short, it is the syntactic sugar of Generator functions.

What is async doing?

Good ~ how he realized to see the friendship link, how to realize the principle? To hell with the interviewer ** Tell him how you did it. See for yourself! ! ! **

All right! I am dozen dozen mouth cannon, the classmates want oneself to see principle ~~, otherwise be asked by the person you can XXX? What would “will” do? “XXXXX” ok, what’s his implementation principle? Men forced in ~ ~ ~

The async function returns a Promise object, and callbacks can be added using the then method. When a function executes, it returns as soon as it encounters await, waits for the asynchronous operation to complete, and then executes the following statement in the function body.

Such as:

async function getStockPriceByName(name) {
  const symbol = await getStockSymbol(name);
  const stockPrice = await getStockPrice(symbol);
  return stockPrice;
}

getStockPriceByName('goog').then(function (result) {
  console.log(result);
});
Copy the code

It comes in many forms

// Function declaration
async function foo() {}

// Function expression
const foo = async function () {};

// Object method
let obj = { async foo() {} };
obj.foo().then(...)

// Class method
class Storage {
  constructor() {
    this.cachePromise = caches.open('avatars');
  }

  async getAvatar(name) {
    const cache = await this.cachePromise;
    return cache.match(`/avatars/${name}.jpg`); }}const storage = new Storage();
storage.getAvatar('jake'). Then (...). ;// Arrow function
const foo = async() = > {};Copy the code

Ok what other parallel usage what of oneself go to see ~~

Let’s take a look at the official explanation of how it works.

The implementation principle of async function

Async functions are implemented by wrapping Generator functions and automatic actuators in one function.

async function fn(args) {
  // ...
}

/ / is equivalent to

function fn(args) {
  return spawn(function* () {
    // ...
  });
}
Copy the code

All async functions can be written in the second form above, where the spawn function is the auto-executor.

Here’s an implementation of the spawn function, which is basically a copy of the autoexecutor above.

function spawn(genF) {
  return new Promise(function(resolve, reject) {
    const gen = genF();
    function step(nextF) {
      let next;
      try {
        next = nextF();
      } catch(e) {
        return reject(e);
      }
      if(next.done) {
        return resolve(next.value);
      }
      Promise.resolve(next.value).then(function(v) {
        step(function() { return gen.next(v); });
      }, function(e) {
        step(function() { return gen.throw(e); });
      });
    }
    step(function() { return gen.next(undefined); });
  });
}
Copy the code

What is the implementation principle of v-Model? If you don’t use the V-model directive, this is how two-way data binding is implemented

V-model’s two-way data binding works so well that it turns out to be nothing more than grammar candy

Probably most children’s shoes all know! But Ruodaun I really haven’t thought about it

But today ~ is to know this record, to urge myself, the wheel can not be used up on the end, investigate its principle, trace back to the source

For example,

<input v-bind:value="iIsFoll" v-on:input="iIsFoll=$event.target.value">
Copy the code

The input event is triggered every time you type, so the input is bound to iIsFoll and the value of iIsFoll is changed

There are three middleware: A, B and C. What is the execution sequence?

  • Since I’ve never used KOA and used express a little bit, I remember that the order of execution is fluid execution, followed by a call to the next middleware with the argument next(),

  • Maybe the interviewer has never used Express before. In fact, express and KOA are designed differently:

  • Express middleware executes sequentially, accustomed to writing response responses in the last middleware;

Koa’s middleware execution order is the “onion ring” model.

He corrected me that it was executed in A circle, that is, A >B > C >B >A, but this is koA execution, commonly known as onion ring

We use this diagram to understand the order of express execution

koa

I won’t say the order of execution

A >B > C >B >A, but this is koA execution, commonly known as’ onion circle

Just refer to it here