记几个前端面试问输出问题
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
推荐阅读
- 家乡的那条小河
- EffectiveObjective-C2.0|EffectiveObjective-C2.0 笔记 - 第二部分
- 野营记-第五章|野营记-第五章 讨伐梦魇兽
- 昨夜小楼听风
- 20170612时间和注意力开销记录
- 2018年11月19日|2018年11月19日 星期一 亲子日记第144篇
- 太平之莲
- 猎杀IP
- Y房东的后半生14
- 眼光要放高远