前端常见面试题

1.数组扁平化

//第一种方法
function flattening(array) {
    for (let i in array) {
        if (array[i] instanceof Array) {
            flattening(array[i])
        } else {
            newArr.push(array[i])
        }
    }
}


//第二种
array.toString().split(',')

2.数组去重

方法有很多,写几种

//第一种方法
let newArr = [];
newArr.push(arr[0]);
for(let i in arr){
    if(newArr.indexOf(arr[i]) === -1){
        newArr.push(arr[i])
    }
}

//第二种方法
let tempArr = [];
for(let i in arr){
    tempArr[arr[i]] = true;
}
let newArr = Object.keys(tempArr);

//第三种方法
let newArr = arr.filter((ele,index,array)=>{
    return index === array.indexOf(ele);
})

//第四种

Array.from(new Set(arr))

3.细节

3.1 toFixed

42.toFixed(3) //SyntaxError .运算符会被优先识别为数字常量的一部分,42. == 42.0
42..toFixed(3) //正确
42 .toFixed(3) //正确

3.2 较小的数值

0.1+0.2 === 0.3 //false 0.1+0.2 = 0.300000000000000004

//polyfill
function numbersCloseEnoughToEqual(n1,n2){
    if(!Number.EPSILON){
        Number.EPSILON = Math.pow(2,-52);
    }
    return Math.abs(n1-n2)<Number.EPSILON;
}
3.3 NaN
NaN === NaN  //false

4.this、作用域、闭包、对象

这两个题来自掘金,原文链接

/**
 * Question 1
 */

var name = 'window'

var person1 = {
  name: 'person1',
  show1: function () {
    console.log(this.name)
  },
  show2: () => console.log(this.name),
  show3: function () {
    return function () {
      console.log(this.name)
    }
  },
  show4: function () {
    return () => console.log(this.name)
  }
}
var person2 = { name: 'person2' }

person1.show1()
person1.show1.call(person2)

person1.show2()
person1.show2.call(person2)

person1.show3()()
person1.show3().call(person2)
person1.show3.call(person2)()

person1.show4()()
person1.show4().call(person2)
person1.show4.call(person2)()

问题1答案:

person1.show1() // person1
person1.show1.call(person2) // person2

person1.show2() // window
person1.show2.call(person2) // window

person1.show3()() // window
person1.show3().call(person2) // person2
person1.show3.call(person2)() // window

person1.show4()() // person1
person1.show4().call(person2) // person1
person1.show4.call(person2)() // person2
/**
 * Question 2
 */
var name = 'window'

function Person (name) {
  this.name = name;
  this.show1 = function () {
    console.log(this.name)
  }
  this.show2 = () => console.log(this.name)
  this.show3 = function () {
    return function () {
      console.log(this.name)
    }
  }
  this.show4 = function () {
    return () => console.log(this.name)
  }
}

var personA = new Person('personA')
var personB = new Person('personB')

personA.show1()
personA.show1.call(personB)

personA.show2()
personA.show2.call(personB)

personA.show3()()
personA.show3().call(personB)
personA.show3.call(personB)()

personA.show4()()
personA.show4().call(personB)
personA.show4.call(personB)()

问题2答案:

personA.show1() // personA
personA.show1.call(personB) // personB

personA.show2() // personA
personA.show2.call(personB) // personA

personA.show3()() // window
personA.show3().call(personB) // personB
personA.show3.call(personB)() // window

personA.show4()() // personA
personA.show4().call(personB) // personA
personA.show4.call(personB)() // personB