Generator
概念
Generator 函数是协程在ES6的实现,最大的特点就是可以交出函数的执行权(即暂停执行)。
generator函数是一个封装的异步任务。执行:
调用generator函数会返回一个iterator,其next方法会返回一个对象,表示当前阶段的信息,包括value属性(yield语句后面表达式的值),done表示是否执行完毕。
例子
1 | var fetch = require('node-fetch'); |
Generator 函数是协程在ES6的实现,最大的特点就是可以交出函数的执行权(即暂停执行)。
generator函数是一个封装的异步任务。执行:
调用generator函数会返回一个iterator,其next方法会返回一个对象,表示当前阶段的信息,包括value属性(yield语句后面表达式的值),done表示是否执行完毕。
1 | var fetch = require('node-fetch'); |
Show the code directly.
myCall:
1 | Function.prototype.myCall = function(ctx) { |
myApply:
1 | Function.prototype.myApply = function(ctx) { |
myBind:
1 | function A() { |
函数A的内部函数B被函数A外的一个变量C所引用
当一个内部函数被其外部函数之外的变量引用时,就形成了一个闭包
1 | function A() { |
在JavaScript中,如果一个对象不再被引用,那么这个对象就会被GC回收,否则一直保存在内存中
B在A中被定义,因此B依赖于A,而外部变量C引用了B,所以A间接被C引用。
1 | (function (document) { |
(function(){})为一个匿名函数,而()执行。
全局对象window引用了obj,而obj依赖于匿名函数,且操作其viewport,故形成闭包。
而当await函数后面跟的是一个异步函数的调用(这里我们强调返回值为异步才为异步函数):
1 | console.log('script start') |
在最新的chrome v8引擎中执行为:
script start
async2 end
Promise
script end
async2 end1
promise1
promise2
async1 end
setTimeout
在这里我们理解为,进入await标记的代码后,将Promise后的首个链式调用注册为micro-task,然后继续执行,直到当前宏任务完成后,微任务也完成后,最后执行await后面的代码,最后再调用其它宏任务。
一个典型的async await的使用为:
我们可以将其简化理解为promise:
『RESOLVE(p)』接近于『Promise.resolve(p)』,不过有微妙而重要的区别:p 如果本身已经是 Promise 实例,Promise.resolve 会直接返回 p 而不是产生一个新 promise。
这里我们复习一下Ajax原生请求:
1 | function search(term, onload, onerror) { |
如果使用Promise对象,可以写成如下:
1 | function search(term) { |
Last a few days, I just looked through some basic rules about prototype and __proto__. But today when I learnt how to implement the function call() by myself, I found that there’s something unclear.
1 | Function.prototype.myCall = function(ctx) { |
How to understand the third line? What does this refer to?
First, let’s recall how we use the function myCall.
1 | Function.prototype.myCall = function(ctx) { |
Wait. As far as I’m concerned, only Object can has a method. But how could a function use a method?
So I looked through some information, and a picture can conclude it well.
So it’s easy for us to understand:
1 | Function.prototype.myCall = function(ctx) { |
So how do we understand the total process of the code?
First we define a method myCall for Function.prototype, then we define a function a, which is inherited from Function.prototype.
In the last line, we make the function a (also an object) call the method myCall. First it look up in a‘s properties, but cannot match, so it backtracks to a.__proto__ AKA Function.prototype. So it finally finds the method, and this refers to the object & function a.
Finally we revise the basic knowledge of prototype chain.
Object.prototype(top of the prototype chain)
Function.prototype inherited from Object.prototype
Function and Object inherited from Function.prototype.
There are some thoughts on the topic which is interestring.
this means where the current property is from.
this points to the object which is the last to calls it.
1 | var name = "windowsName"; |
1 | var name = "windowsName"; |
1 | var name = "windowsName"; |
1 | var name = "windowsName"; |
this in arrow function points where the function is defined, instead of conducting.
If there is no this in arrow function, we must find the scope chain to find the value. if a non-arrow function contains the arrow function, this in the arrow function is the this in the non-arrow function, otherwise this is undefined.
1 | var name = "windowsName"; |
1 | var name = "windowsName"; |
apply & call difference:
1 | fun.apply(thisArg, [argsArray]) |
How to understand javascript new operation?
A easy example:
1 | function soldier(ID) { |
Using new, we can prevent doing the following things:
After we using new :
1 | function soldier(ID) { |
There is a property named constructor:
So we need to define the property seperatedly.
First let’s do a test:
1 | function Test(name) { |
Test if we return a value in the function:
1 | function Test(name) { |
So if we return a value in the function, there is no difference.
Test if we return an object in the function:
1 | function Test(name) { |
So if we return an object, the new doesnt effect.
1 | function create(fun, ...args) { |
Then we test function create :
When it comes to inheritance, there is only one data type in js: object. Every object has a private property named \_proto___ which points to its prototype. The top of the prototype is Object. Object.__proto__ === null
1 | // 让我们从一个函数里创建一个对象o,它自身拥有属性a和b的: |
1 | function doSomething(){} |
1 | { |
1 | function foo() {} |
1 | function foo(){} |
IE8 unsupported
perform badly
need abandoning
prototype
和 Object.getPrototypeOf
How to implement an add funtion?
How to implement add(1)(3) === 4?
How to implement add3(2) === 5?
1 | function currying(fn, ...args1) { |
How to implement infinite arguments?
1 | function trueCurrying(fn, ...args1) { |