女王控的博客

笔试实录

联通产业交互

this

写出执行结果

js 复制代码
(function() {
  var obj = {
    fun1: () => {
      console.log(this);
    },
    fun2: function() {
      console.log(this);
    },
    fun3() {
      console.log(this);
    }
  };

  obj.fun1();
  obj.fun2();
  obj.fun3();
})();
js 复制代码
window; // 箭头函数的 this 根据外层作用域绑定
obj; // 隐式绑定,this 指向对象
obj; // 同上,不同写法而已

作用域

js 复制代码
(function() {
  var a = '';
  console.log(a);
  console.log(b);
  {
    var b = (a = '123');
    let c = b;
  }
  console.log(c);
})();
js 复制代码
'';
undefined;
ReferenceError;

异步

js 复制代码
async function async1() {
  console.log('1');
  await async2();
  console.log('2');
}
async function async2() {
  console.log('3');
}
console.log('4');
setTimeout(function() {
  console.log('5');
}, 0);
async1();
new Promise(function(resolve) {
  console.log('6');
  resolve();
}).then(function() {
  console.log('7');
});
console.log('8');
js 复制代码
4;
1;
3;
6;
8;
2;
7;
5;

闭包

js 复制代码
var count = 10;
function add() {
  var count = 0;
  return function() {
    count += 1;
    console.log(count);
  };
}
var s = add();
s();
s();
js 复制代码
1;
2;

阅读下一篇

多维分析架构解析
2021-03-29 10:02:10
0%