js基础题

博客围绕JavaScript展开,涉及数组引用地址、函数默认值作用域、队列与宏任务微任务、不同作用域区别等知识点,还要求写出相关执行结果,同时提及实现数组flat函数和deepGet函数的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

必须得会啊(o)/~

  1. 写出执行结果
var name = "Word";
(function () {
  if (typeof name === 'undefined') {
    var name = 'Jack'
    console.log('Goodbye' + name)
  } else {
    console.log('Hello' + name)
  }
})()
// 结果:GoodbyeJack
  1. 写出执行结果
console.log([1,2,3].map(x => x + 1))
// 结果:[2, 3, 4]
  1. 写出执行结果
const numbers = (a, ...b) => [a, b]
console.log(numbers(1,2,3,4,5))
// 结果:[ 1, [ 2, 3, 4, 5 ] ]
  1. 写出执行结果
var foo = 'bar'
console.log({foo})
// 结果:{ foo: 'bar' }
  1. 写出执行结果
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
  1. 写出执行结果
let a = 'b'
let c = {
  [a]: 'd'
 }
 console.log(c)
 // 结果:{ b: 'd' }
  1. 写出执行结果
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)
  1. 写出执行结果
let {x, y, ...z} = {x: 1, y: 2, a: 3, b: 4}
console.log(z)
// 结果:{ a: 3, b: 4 }
  1. 写出执行结果
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
  1. 写出执行结果
const myMap = new Map()
                  .set(true, 7)
                  .set({foo:3}, ['abc']);
console.log([...myMap])
// 结果:[ [ true, 7 ], [ { foo: 3 }, [ 'abc' ] ] ]
  1. 写出执行结果
const arr = ['a', 'b', 'c'].map
const map = arr.bind([1,2,3])
map(el => console.log(el))
// 结果: 1 2 3
  1. 写出执行结果
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
  1. 写出执行结果
const mySet = new Set([{a:1}, {a: 1}])
const result = [...mySet]
console.log(result)
//结果:[ { a: 1 }, { a: 1 } ]
  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
  1. 写出执行结果
console.log(
  arr1.sort() === att1,
  arr2.sort() == arr2,
  arr1.sort() === arr2.sort()
)
// 结果:true,true, false

即使数组内容改变了,但是引用地址并没有发生变化

  1. 写出执行结果
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在这里插入图片描述

  1. 写出执行结果
for(var i = 0; i<5;i++) {
  setTimeout(function() {console.log(i)}, i*1000)
}
// 结果; 5, 5, 5, 5, 5

是在间隔同等时间下打印的五个5,也涉及到队列和宏任务微任务

  1. 写出执行结果
(function(x) {
  return (function(y) {
    console.log(x)
  })(2)
})(1)
// 结果: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))
  1. 实现deepGet函数

  2. 写出执行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值