这些前端面试题,你能全答对吗?

177 阅读2分钟

前言

答案我就不写了,各位大佬可以在浏览器中运行查看执行结果。也可以评论区留言,交流一下遇到的奇葩面试题。

1.

class Person{
    constructor(age, name) {
        this.age = age
        this.name = name
    }
}

let a1 = new Person(21, 'test') 

let a2 = Person(22, 'wang')

console.log(a1) 
console.log(a2) 
// A. Person { age: 21, name: 'test' } TypeError:
// B. Person { age: 21, name: 'test' } Person { age: 22, name: 'wang' }

2.

class Person{
    constructor(age, name) {
        this.age = age
        this.name = name
        this.getX = () => {
            return this.age * 1
        }
    }

    getX() {
        return this.age * 2
    }
}

let a1 = new Person(5, 'test')

Person.prototype.getX = function() {
    return this.age * 3
}

console.log(a1.getX())
// A. 5
// B. 10
// C. 15
// D. Error
class Person{
    constructor(age, name) {
        this.age = age
        this.name = name
    }

    getX() {
        return this.age * 2
    }
}

let a1 = new Person(5, 'test')

Person.prototype.getX = function() {
    return this.age * 3
}

console.log(a1.getX())
// A. 5 
// B. 10 
// C. 15 
// D. Error

3.

class Animal {
  speak() {
    return this;
  }
  static eat() {
    return this;
  }
}

let obj = new Animal();
obj.speak();
let speak = obj.speak;
console.log(speak());  

Animal.eat() 
let eat = Animal.eat;
console.log(eat()); 

// A. undefined Animal {}
// B. undefined undefined
// C. Animal {} Animal {}
// D. Animal {} undefined

4.

const a = true, b = '2', c = 3

console.log(a + c - b)
console.log(a + b - c)
// A. 2 NaN
// B. true1 true-1
// C. 2 -1
// D. true1 -1

5.

const a = Promise.resolve('resolve1')

async function test(){
    return await a
}

console.log(test())
// A. Promise { <pending> }
// B. Promise { 'resolve1' }
// C. Promise {}

6.

Promise.reject("error")
  .catch((error) => console.log(error))
  .then((error) => console.log(error));
  
// A. error error
// B. error undefined
// C. error

7.

for(let i = 0; i < 3; i++) {
    let i = 'test'
    console.log(i)
}
// 结果为:

8.

let a = 1
let a = 2

console.log(a)
// 结果为:

9.

<form class="file" data="file">
    <input id="file" name="file"> 
</form>

A. document.querySelectorAll('file')[0]
B. document.getElementById('#file')
C. document.getElementsByTagName('file')[0]
D. document.querySelector('#file')

10.

function test() {
    try {
        throw 'error'
        return 1
    } finally {
        return 2
    }
}

console.log(test())
// A. 1
// B. 2
// C. error
// D. Error:...

11.

var a = 2, b
(function() {
    console.log(a)
    console.log(b)
    var a = b = 5
    console.log(a)
    console.log(b)
})()
console.log(a)
console.log(b)
// 结果为:

12.

const a = {1: '1', 2: '2', 3: { 4: '4', 5: { 6: '6' }}}

const b = JSON.stringify(a)

JSON.parse(b, function(k, v){
    console.log(k)
    return v
})
// 结果为:

13.

console.log(1 || 2 && 3)

console.log(1 && 2 || 3)

console.log(1 && 2 && 3)

// 结果为:

14.

function foo() {
    function bar() {
        console.trace();
    }
    bar();
}

foo();

// 结果为:

15.

typeof console

typeof console.log

typeof console.log(a = 1)

typeof typeof console.log(a = 1)

// 结果为:

16.

['1','2','3'].map(parseInt)

// A. [ 1, 2, 3 ]
// B. TypeError:...
// C. [ NaN, NaN, NaN ]
// D. [ 1, NaN, NaN ]

17.

let map = new Map()
map.set(1, 1)
map.set(3, 3)
map.set(2, 2)
for(let v in map) {
    console.log(v) // 结果为:
}
for(let v of map) {
    console.log(v) // 结果为:
}

18. Set

let set = new Set()
set.add(1)
set.add(3)
set.add(2)
set.add(3)
for(let v in set) {
    console.log(v) // 结果为:
}
for(let v of set) {
    console.log(v) // 结果为:
}

19. 引用传递

let info = { a: 1 }

function test(info) {
  let b = info.a
  b = 2
}

test(info)

console.log(info.a)
let info = { a: 1 }

function test(info) {
  let b = {}
  b.a = info.a
  b.a = 2
}

test(info)

console.log(info.a)