共计 1101 个字符,预计需要花费 3 分钟才能阅读完成。
this 指向
1.
function foo() {console.log( this.a);
}
function doFoo() {foo();
}
var obj = {
a: 1,
doFoo: doFoo
};
var a = 2;
obj.doFoo() // 2
2.
var inner = 'window'
function say() {console.log(inner)
console.log(this.inner)
}
let obj = (function f() {
var inner = 'inner-1'
return {
inner: 'inner-2',
say: function(){console.log(inner);
console.log(this.inner);
}
}
})()
say() // window, window
obj.say() // inner-1 inner-2
obj.say = say
obj.say() // window inner-2
事件循环
1.
setTimeout(() => {console.log('1');
Promise.resolve().then(() => {console.log('2');
});
}, 0);
new Promise((resolve) => {console.log('3');
resolve();}).then(() => {console.log('4');
setTimeout(() => {console.log('5');
}, 0);
}).then(() => {console.log('6');
});
console.log('7');
// 3 7 4 6 1 2 5
2.
async function async1() {console.log('async1 start');
await async2();
console.log('async1 end');
}
async function async2() {console.log('async2');
}
console.log('script start');
setTimeout(function() {console.log('setTimeout');
}, 0);
async1();
new Promise(function(resolve) {console.log('promise1');
resolve();}).then(function() {console.log('promise2');
});
console.log('script end');
// script start, async1 start, async2, promise1, script end, async1 end, promise2, setTimeout
正文完
发表至: javascript
2021-09-08