【前端面试】一份考察基础、有点绕、带陷阱的JS题

46 阅读3分钟

前阵子面试做的,拍了下来现在手码给大家,原共45道单选题,限30分钟,现去掉几道概念背诵题和白痴题,保留代码题;仅保留题目,无选项

1:

const promise1 = Promise.resolve('1')
    const promise2 = Promise.resolve('2')
    const promise3 = Promise.reject('3')
    const promise4 = Promise.resolve('4')
    const run = async () => {
        const res1 = await Promise.all([promise1, promise2])
        const res2 = await Promise.all([promise3, promise4])
        return [res1, res2]
    }
    run().then(res => console.log(res))
    .catch(err => console.log(err))
    
    输出什么

2:

String.prototype.give = () => {
        return 'haha'
    }
    const name = 'fish'

    name.give()
    
    输出什么

3:

  function test(){
        return 'haha'
    }
  const test2 = () => 'xixixi'

    console.log(test.prototype)
    console.log(test2.prototype)
    
    输出什么

4:

  function test(a, b){
        const forma = new Intl.NumberFormat('en-US', {style: 'unit', unit: 'mile-per-hour'}).format(a) 
        const forma2 = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD'}).format(b)
        return `test ${forma} haha ${forma2}`
    }
    console.log(test(130, 300))
    
    输出什么

5:

const test = {name: a}
    console.log(test.color[1])
输出什么

6:

let list = [1,2,3].push(4)   
    console.log(list.push(5))
    
    输出什么

7:

   function test(p1, p2 = p) {
        if(p1 !== p2) {
            console.log('aaa')
        } else {
            console.log('bbb')
        }
    }
    const p = {name: 'fish'}

    test(p)
    
    输出什么

8:

parseInt('7*6', 10)
输出什么

9:

  const info = {
        [Symbol('a')]: 'b'
    }
    console.log(info)
    console.log(Object.keys(info))
    
    输出什么

10:

  class P {
        constructor(name) {
            this.name = name
        }
    }
    const a = new P('fish')
    console.log(typeof a)
    输出什么

11:

function test(a, b) { return a + b}
    // 是否为纯函数

12:

Number(2) === Number(2)
    Boolean(false) === Boolean(false)
    Symbol('foo') === Symbol('foo')
    
    T or F ?

13:

const aList = ['a', 'b', 'c'];
    ({item: aList[3]} = {item: 'd'})
    console.log(aList)
    
    输出什么

14:

 function test(){
        console.log('aaa')
    }
    test.name = 'fish'
    输出什么

15:

 // js全局执行上下文为你做了两件事:全局对象和this关键字
    // 错、对、看情况

16:

   let num = 10;
    const fa = () => num++;
    const fb = number => number++;

    const num1 = fa()
    const num2 = fb(num1)

    console.log(num1)
    console.log(num2)
    输出什么

17:

 const add = x => x + x;
    function test(num = 2, value = add(num)) {
        console.log(num, value)
    }
    test()
    test(3)
    输出什么

18:

 const pp = {
        name: 'fish',
        value: ['a']
    }
    function add(a, b = pp.value) {
        b.push(a)
        return b
    }
    add('bb', [])
    add('cc')
    add('dd', pp.value) 

    console.log(pp.value)
    输出什么

19:

 function test(a, b, c) {
        console.log(a)
        console.log(b)
        console.log(c)
    }
    const p1 = 'fish'
    const age = 21

    test`${p1} is ${age} hahaha`

输出什么

20:

 const obj = {
        1: 'a',
        2: 'b',
        3: 'c'
    }
    const set = new Set([1,2,3,4]);

    obj.hasOwnProperty('1')
    obj.hasOwnProperty(1)
    set.has('1')
    set.has(1)
    
    // T or F ?

21:

  const value = {number : 10}
    const fun = (x = {...value}) => {
        console.log(x.number *= 2)
    }
    fun() 
    fun()
    fun(value)
    fun(value)
    
    输出什么

22:

 //  下面哪些是false
    0
    new Number(0)
    ('')
    (' ')
    new Boolean(false)
    undefined

23:

  const name2 = 'fish'
    console.log(name2())

输出什么

24:

    const teams = [        {name: 'fish', value: ['aa', 'bb']},
        {name: 'bee', value: ['cc', 'dd']},
    ]

    function* getValue(value){
        for(let i = 0; i < value.length; i++) {
            yield value[i]
        }
    }

    function* getTeams(teams){
        for(let i = 0; i < teams.length; i++) {
            // 要怎么填,才能确保输出以下打印
        }
    }

    const obj = getTeams(teams)
    obj.next() // {value: 'aa', done: false}
    obj.next() // {value: 'bb', done: false}

25:

    class Bird {
        constructor() {
            console.log('aaa')
        }
    }

    class bb extends Bird {
        constructor() {
            console.log('bbb')
            super()
        }
    }

    const pet = new bb()
    输出什么?

26:

 var status = 'a'
    setTimeout(()=>{
        const status = 'b'
        const data = {
            status: 'c',
            getStstus() {
                return this.status
            }
        }
        console.log(data.getStstus())
        console.log(data.getStstus.call(this))
    }, 0)
    
    输出什么

27:

 const getList = ([x, ...y]) => [x, y]
    const getUser = user => {
        return { name:user.name, age: user.age }
    }

    const list = [1,2,3,4]
    const user = {
        name: 'fish',
        age: 21
    }

    console.log(getList(list))
    console.log(getUser(user))
    
    输出什么

28:

var a1 = 10, a2 = 20;
    alert("a1+a2="+a1+a2)
    输出什么

这份笔试题我挂了的,60分及格,原共45道,限时30分钟,刚好我53分,虽然是选择题,很多代码题我都得细读再回忆知识点,说到底还是基础差,但也有部分题让我感到很莫名其妙,做半小时后都还有十多道题没填,直接摆烂把题都拍下来~