必须得会啊(o)/~
- 写出执行结果
var name = "Word";
(function () {
if (typeof name === 'undefined') {
var name = 'Jack'
console.log('Goodbye' + name)
} else {
console.log('Hello' + name)
}
})()
// 结果:GoodbyeJack
- 写出执行结果
console.log([1,2,3].map(x => x + 1))
// 结果:[2, 3, 4]
- 写出执行结果
const numbers = (a, ...b) => [a, b]
console.log(numbers(1,2,3,4,5))
// 结果:[ 1, [ 2, 3, 4, 5 ] ]
- 写出执行结果
var foo = 'bar'
console.log({foo})
// 结果:{ foo: 'bar' }
- 写出执行结果
const f = (x,y) => ({x,y})
console.log(f(1,2)) // 结果:{ x: 1, y: 2 }
const f2 = (x,y) => {x,y}
console.log(f2(1,2)) // 结果:undefined
- 写出执行结果
let a = 'b'
let c = {
[a]: 'd'
}
console.log(c)
// 结果:{ b: 'd' }
- 写出执行结果
var target = {a:{b:'c',d:'e'}}
var source = {a:{b:'hello'}}
Object.assign({}, target, source) // 结果:{ a: { b: 'c', d: 'e' } }
Object.assign(target, source) //结果:{ a: { b: 'hello' } }
console.log(target)
- 写出执行结果
let {x, y, ...z} = {x: 1, y: 2, a: 3, b: 4}
console.log(z)
// 结果:{ a: 3, b: 4 }
- 写出执行结果
const arr = [x => x*1, x => x*2, x => x*3, x => x*4]
console.log(arr.reduce((agg, el) => agg + el(agg), 1))
// 结果:1+1* 1=2+2*2=6+6*3=24+24*4=120
- 写出执行结果
const myMap = new Map()
.set(true, 7)
.set({foo:3}, ['abc']);
console.log([...myMap])
// 结果:[ [ true, 7 ], [ { foo: 3 }, [ 'abc' ] ] ]
- 写出执行结果
const arr = ['a', 'b', 'c'].map
const map = arr.bind([1,2,3])
map(el => console.log(el))
// 结果: 1 2 3
- 写出执行结果
function Dog(name) {
this.name = name
this.speak = function(){
return 'woof'
}
}
const dog = new Dog('Pogo')
Dog.prototype.speak = function(){
return 'arf'
}
console.log(dog.speak())
// 结果:woof
- 写出执行结果
const mySet = new Set([{a:1}, {a: 1}])
const result = [...mySet]
console.log(result)
//结果:[ { a: 1 }, { a: 1 } ]
- 写出执行结果
console.log(1)
new Promise(function (resolve, reject){
reject(true)
window.setTimeout(function (){
resolve(false)
}, 0)
}).then(function(){
console.log(2)
}, function(){
console.log(3)
})
// 结果:1, 3
- 写出执行结果
console.log(
arr1.sort() === att1,
arr2.sort() == arr2,
arr1.sort() === arr2.sort()
)
// 结果:true,true, false
即使数组内容改变了,但是引用地址并没有发生变化
- 写出执行结果
var x = 1
function func(x,y = function anonymous(){x=2}){
var x = 3
y()
console.log(x)
}
func(5)
console.log(x)
//结果: 3, 1
如果函数设置了默认值,就形成了三个作用域,参考阮一峰的ES6教程https://es6.ruanyifeng.com/#docs/function
- 写出执行结果
for(var i = 0; i<5;i++) {
setTimeout(function() {console.log(i)}, i*1000)
}
// 结果; 5, 5, 5, 5, 5
是在间隔同等时间下打印的五个5,也涉及到队列和宏任务微任务
- 写出执行结果
(function(x) {
return (function(y) {
console.log(x)
})(2)
})(1)
// 结果:1
区分函数作用域和全局作用域和块作用域的区别,以及立即执行函数的作用域的特殊情况
- 实现数组的flat函数的实现逻辑
const arr = [
[1,2],
[3,[4,5]],
6, 9
]
const newArr = [] // 存放新数组
function flat(arr, depth=1){
if(depth <= 0) {
newArr.push(arr)
return
}
arr.forEach(it => {
if(Array.isArray(it)){
depth--
flat(it, depth)
}else {
newArr.push(it)
}
})
}
console.log(flat(arr, 2))
-
实现deepGet函数
-
写出执行结果