event loop OR this

67 阅读1分钟
console.log('1');
async function async1() {
    console.log('2');
    await console.log('3');
    console.log('4');
}

async1();

setTimeout(() => {
    console.log('5');
}, 0);

new Promise((resolve, reject) => {
    console.log('6');
    resolve();
}).then(() => {
    console.log('7');
});

console.log('8');

// 1
// 2
// 3
// 6
// 8
// 4
// 7
// 5
// eslint-disable-next-line no-var
var name = 'window';
// eslint-disable-next-line no-var
var obj = {
    name: 'obj',
    normal() {
        return () => {
            console.log('this.name :', this.name);
        };
    },
    arrow: () => {
        return function() {
            console.log('this.name :', this.name);
        };
    }
};
// eslint-disable-next-line no-var
var obj1 = {name: 'obj1'};

obj.normal.call(obj1)();
obj.arrow.call(obj1)();

// node:
// this.name : obj1
// this.name : undefined

// 浏览器
// this.name : obj1
// this.name : window