记一次前端面试题

if (!("a" in window)) {
  var a = 1;
}
console.log(a);
复制代码
var a = 1,
  b = function a(x) {
    x && a(--x)
  };
console.log(a);
复制代码
function b(x, y, a) {
  a = 8;
  arguments[2] = 10;
  console.log(a);
}
b(1, 2, 3);
复制代码
(function f(f) {
  console.log((typeof f()));
})(function () { return 1; })
复制代码
var car = {
  price: 15,
  run: function () {
    return this.price;
  }
}
console.log(typeof (f = car.run)());
复制代码
var a = 1;
if (function () {}) {
  a += typeof f;
}
console.log(a);
复制代码
var obj = {
  value: 3,
  fun: function () {
    return this.value;
  }
};
var fun = function () {
  this.value = this.fun.apply(window) + "1";
}
fun.prototype = obj;
console.log(new fun().value);
复制代码
class A {
  constructor() {
    this.a = 1;
  }
  aFun() {
  }
}
class B extends A {
  constructor() {
    super();
    this.b = 2;
  }
  bFun() {
  }
}
let obj = new B();
console.log(window.B);
复制代码
let arr = [];
setTimeout(function () {
  arr.push(1);
});
Promise.resolve().then(() => {
  arr.push(2);
});
new Promise(() => {
  arr.push(7);
});
async function a() {
  arr.push(3);
  await b();
  arr.push(4);
}
async function b() {
  arr.push(5);
}
a();
arr.push(6);
setTimeout(() => {
  console.log(arr.join(''));
});
复制代码
  1. 用promise写一个retry方法,如一个接口最多请求10次,当有请求成功时,就不在请求
function retry(fun, num) {
  if (num <= 0) return;
  fun().then(res => {
    console.log(res, num);
  }).catch(() => {
    retry(fun, num - 1)
  })
 }
 retry(function () {
  return new Promise(function (resolve, reject) {
    let flag = Math.random() * 10;
    if (flag > 5) {
      resolve('okok');
    } else {
      reject();
    }
  })
}, 10)
复制代码

转载于:https://juejin.im/post/5bce7f7c51882577583160f3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值