JavaScript -- 刷新你对JavaScript的题目

120 阅读1分钟

1. 块级作用域下声明函数的问题

var a = 10
{
  a = 50
  function a() {}
  a = 20
}
console.log(a)

2. js隐式类型转换(1)

// 如何保证正常的打印
if (a === 1 && a === 2 && a === 3) {
  console.log('进来了')
}

3. js隐式类型转换(2)

// 打印输入了什么
if (function f () {}) {
   console.log(f)
 }

4. js隐式类型转换(3)

  // 分别打印了啥
  len1 = ([] + []).length
  len2 = ({} + {}).length
  len3 = (function () {}).length
  console.log(len1)
  console.log(len2)
  console.log(len3)

5. Function构造器的基础知识问题(1)

  var a = 1,
  	  b = 2
  function fun() {
    var b = 3
    return new Function('c', 'console.log(a + b + c)')
  }
  var test = fun()
  test(4) // 结果是什么

6. Function构造器的基础知识问题(2)

  // 分别打印什么
  var fun1 = new Function('console.log('fun1')')
  var fun2 = Function('console.log('fun2')')
  fun1()
  fun2()
  console.log(Function.__proto__ === Function.prototype)

7. typeof

  // 分别输出啥
  console.log(typeof a)
  console.log(a)

8. async await执行顺序

    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')
    // 输出结果

9. 涉及到很多知识点的面试题

function Foo() {
    getName = function () { alert (1); };
    return this;
}
Foo.getName = function () { alert (2);};
Foo.prototype.getName = function () { alert (3);};
var getName = function () { alert (4);};
function getName() { alert (5);}

Foo.getName();
getName();
Foo().getName();
getName();
new Foo.getName();
new Foo().getName();
new new Foo().getName();

10. 自动补全 ASI

// 下面代码分别输出什么
{} + 10
({} + 10)