手记

前端常见面试题总结1

这里就先针对let、this相关的题,可能比较类似,针对性的多做相似题加深印象。争取这一次知其然更知其所以然(思路只简单提醒,有问题请留言,答案在文档尾)

  1. 使用let声明的变量不与window产生映射
	let n = 10,
    obj = { n: 20 };
let fn = obj.fn = (function () {
    this.n++;
    n++;
    return function (m) {
        n += 10 + (++m);
        this.n += n;
        console.log(n)
    }
})(obj.n)
fn(10);
obj.fn(10);
console.log(n, obj.n)
  1. undefined或者NaN与数值相加 结果都为NaN
    undefined与字符串相加,先转换成字符串再与字符串相加操作
let n = 1;
let x = {
    n: 2,
    y: (function (n) {
        n = n || 3;
        return function (m) {
            m = m || 4;
            this.n += m++;
            n += ++m;
            console.log(n)
        }
    })(window.n)
};
let z = x.y;
x.y(5);
z(6);
console.log(n, x.n)

3.注意++n 和n++区别

let i = 0;
let fn = function (n) {
    i += 2;
    return function (m) {
        i += (++n) + (m--);
        console.log(i)
    }
};
let f = fn(2);
f(3);
fn(2)(3)
f(4)
console.log(i)

4.注意 -=的运算展开

let i = 2;
let fn = function (n) {
    i *= 2;
    return function (m) {
        i -= (n--) + (++m);
        console.log(i)
    }
};
let f = fn(1)
f(2);
fn(3)(4);
f(5)
console.log(i)
  1. 变量提升和词法分析
if ('m' in window) {
    var m = m && 12;
}
console.log(m);

let n = 10;
if (!('n' in window)) {
    let n = n + 30
}
console.log(n)

答案

  1. 32
    53
    53 73
  2. 10
    18
    1 7
  3. 8
    16
    24
    24
  4. 0
    -8
    -14
    -14
  5. undefined
    报错:n is not defined
0人推荐
随时随地看视频
慕课网APP