本文共 4038 字,大约阅读时间需要 13 分钟。
本博客部分题目来自B站UP主
笔者觉得有些题目不错,记录一下。const a = (b)=>{ return b instanceof Function ? b() : b}console.log(a) console.log(a(()=>'hello'))console.log(a('world'))
.
. . . . . 答案:a函数本身,“hello”,“world”const myFunc = str=>{ if(str.length > 1){ return myFunc(str.slice(1)) } return str}console.log(myFunc('hello world'))
.
. . . . . 答案:“d”const obj = { 1:'1', 2:'2', 3:'3'}console.log(Object.keys(obj), Object.values(obj))console.log(Object.keys(obj) == Object.values(obj))console.log(Object.keys(obj) == Object.keys(obj))
.
. . . . . 答案:[ ‘1’, ‘2’, ‘3’ ] [ ‘1’, ‘2’, ‘3’ ] false false tips:虽说两个数组字面量相等,但是地址不同,所以不相等const arr = [...new Set([3, 1, 2, 3, 4])]console.log(arr.length, arr[2])
.
. . . . . 答案:4 2const map = ['a', 'b', 'c'].map.bind([1, 2, 3])map(el => console.log(el))
.
. . . . . 答案:1,2,3 tips:相当于map = Array.prototype.map.bind([1, 2, 3])
,与前面的字符数组无关 const map = ['a', 'b', 'c'].map.bind([1, 2, 3])map(el => console.log(el))
.
. . . . . 答案:1,2,3 tips:相当于map = Array.prototype.map.bind([1, 2, 3])
,与前面的字符数组无关 const arr1 = [{ firstName:'james'}]const arr2 = [...arr1]arr2[0].firstName = 'zed'console.log(arr1)
.
. . . . . 答案:[ { firstName: ‘zed’ } ]const arr = [ x => x * 1, x => x * 2, x => x * 3, x => x * 4]console.log(arr.reduce((acc, cur) => acc + cur(acc), 1))
.
. . . . . 答案:120const timer = a =>{ return new Promise(resolve => { setTimeout(()=>{ resolve(a) }, Math.random() * 100) })}Promise.all([ timer('hello'), timer('world')]) .then(data => { console.log(data) })
.
. . . . . 答案:[ ‘hello’, ‘world’ ] tips:Promise.all([…]) 的结果顺序是按照Promise数组的顺序来的,爬虫中乱序不是all的原因,是并发的多个getOne
函数的请求时间不同,进入Promise数组的顺序也就是乱序的 function Dog(name){ this.name = name this.speak = function() { return 'woof' }}const dog = new Dog('lishuai')Dog.prototype.speak = function() { return 'arf'}console.log(dog.speak())
.
. . . . . 答案:woofconst user = { name: 'Joe', age: 25, pet: { type: 'dog', name: 'lishuai' }}Object.freeze(user)user.pet.name = 'zed'console.log(user.pet.name)
.
. . . . . 答案:lishuai tips:Object.freeze
是浅冻结 const mySet = new Set([{ a:1},{ a:1}])console.log([...mySet])
.
. . . . . 答案:[ { a: 1 }, { a: 1 } ] tips:字面量相同的引用类型数据,不会被Set
当做同一个数据。 const arr1 = ['a', 'b', 'c']const arr2 = ['c', 'b', 'a']console.log( arr1.sort() === arr1, arr2 == arr2.sort(), arr1.sort() === arr2.sort())
.
. . . . . 答案:true true false tips:Array.prototype.sort
改变原数组,所以第一行和第二行是true;第三行数组的字面量虽然相同,但是内存地址不同 const val = 1//第一种方式console.log(Number.isInteger(val))//第二种方式console.log(isInt(val))//第三种方式function isInt(val){ return Math.round(val) === val}
let arr = new Array(3)arr = arr.map(() => 1)console.log(arr)
.
. . . . . 答案:[ < 3 empty items > ] tips:Array.prototype.map
不会遍历空数组 var uname = 'jack'function change() { alert(uname) // ? var uname = 'lily' alert(uname) //?}change()
.
. . . . . 答案:undefined lily 解释:因为使用var
声明的变量,存在变量声明提升特性,在change
函数中,就相当与,第一行有var uname
,在打印这个未赋值的uname
后,就uname = lily
,然后在打印。 var trim = function (str){ var index = 0 var index1 = str.length-1 while (str[index] === ' ') { index++; } while (str[index1] === ' '){ index1-- } return str.slice(index,index1 + 1) } console.log(trim(' 1 2 3 '))
const theOftenChar = (str)=>{ const query = { } let max = -1 let maxKey; for (let item of str){ if (typeof query[item] === 'undefined'){ query[item] = 1 }else { query[item]++ } } for (let key in query){ if (query[key] > max){ max = query[key] maxKey = key } } return maxKey }
转载地址:http://brozi.baihongyu.com/