【面经】字节前端开发工程师一面代码题面经
1. 作用域
let value = 1;
function foo() {
console.log(value);
}
function bar() {
let value = 2;
foo();
}
bar() //1 2.this指向
window.name = 'ByteDance';
function A () {
this.name = 123;
}
A.prototype.getA = function () {
return this.name + 1;
};
let a = new A();
let funcA = a.getA;
console.log(funcA());//ByteDance1
console.log(a.getA());//124
3.this指向
const obj = {
birth: 1990,
getAge: function (year) {
let fn = y => y - this.birth;
return fn.call({ birth: 2000 }, year);
}
};
console.log(obj.getAge(2020)); ***循环
async function async1() {
console.log('async1 start'); //2
await async2();
console.log('async1 end'); //6
async function async2() {
console.log('async2'); //3
}
}
console.log('script start'); // 1
setTimeout(function () {
console.log('setTimeout');//8
}, 0);
async1();
new Promise(function (resolve) {
console.log('promise1'); //4
resolve();
}).then(function () {
console.log('promise2'); //7
});
console.log('script end'); //5