编写高质量代码 - 面试题

parseInt

parseInt默认采用十进制,以0x开头的默认为十六进制

['1', '2', '3'].map(parseInt) 相当于
['1', '2', '3'].map((item, index) => parseInt(item, index)) // 输出[1, NaN, NaN]
['1', '2', '3'].map(item => parseInt(item)) // 输出 [1, 2, 3]

函数参数赋值传递

function changeArg(x) {
     x = 200 }

let num = 100
changeArg(num)
console.log('num:', num)

let obj = {
    name: '小名' }
changeArg(obj)
console.log('obj:', obj)
// num: 100, obj: {name: '小名'}
1. num = 100, x = num
2. obj = {
    name: '小名' }, y = obj
传递的是值不是变量,不会变

手写一个js函数,实现数组扁平化ArrayFlatten

// 一层扁平化
function arrayFlat(arr) {
   
    const flatArr = []
    arr.forEach(item => {
   
        if (Array.isArray(item)) {
   
            item.forEach(v => {
    flatArr.push(v) })
        } else {
   
            flatArr.push(item)
        }
    })
    return flatArr
}

const arr = [1, [2, [3]], null, {
    11: 12 }]
const a = arrayFlat(arr)
// [1, 2, Array(1), null, {…}]
// 深度扁平化
function arrayFlat(arr) {
   
    const flatArr = []
    arr.forEach(item => {
   
        if(Array.isArray(item)) {
   
            const flatItem = arrayFlat(item)
            flatItem.forEach(v => flatArr.push(v))
            // flatArr = flatArr.concat(flatItem)
        } else {
   
            flatArr.push(item)
            // flatArr = flatArr.concat(item)
        } 
    })
    return flatArr 
} 
// 自己写的方法1
const flatArr = []
function arrayFlat(arr) {
   
    arr.forEach(item => {
   
        if(Array.isArray(item)) {
   
            arrayFlat(item)
        } else {
   
            flatArr.push(item)
        } 
    })
}
// 自己写的方法2
function ArrayFlatten(data, flatArr = []) {
   
  data.forEach(item => {
   
    if (Array.isArray(item)) {
   
      return ArrayFlatten(item, flatArr)
    } else {
   
      flatArr.push(item)
    }
  })
  return flatArr
}
const arr = [[1, 2, 3], [[4, 5, 6]], ['arr'], null]
arrayFlat(arr)
// [1, 2, 3, null, {…}]

手写一个getType函数,获取详细的数据类型

// 使用 Object.prototype.toString.call(x)
// 注意,不能直接调用 x.toString()
function getType(filed) {
   
    const originType = Object.prototype.toString.call(filed) // [Object String]
    const spaceIndex = originType.indexOf(' ')
    const type = originType.slice(spaceIndex + 1, -1) // String
    return type.toLowerCase()
}
// 自己写的 - 枚举踩雷
// 你可能忽略某些类型
// 增加了新类型,需要修改代码
function getType(filed) {
   
    const type = undefined
    if (typeof filed === 'number') {
   
        type = 'number'
    } else if (typeof filed === 'string') {
   
        type = 'string'
    } else if (typeof filed === 'symbol') {
   
        type = 'symbol'
    } else if (typeof filed === 'boolean') {
   
        type = 'boolean'
    } else if (typeof filed === 'function') {
   
        type = 'function'
    } else if (typeof filed === 'object') {
   
        if (filed = null) {
   
            type = 'null'  
        } else if (filed instanceof Object) {
   
            type = 'object'
        } else if (filed instanceof Array) {
   
            type = 'array'
        }  else if (filed instanceof Map) {
   
            type = 'map'
        } 
    }
    return type 
}

在这里插入图片描述

new一个对象的过程是什么,手写代码表示

class是function的语法糖

// function形式
function Foo(name, age) {
   
    this.name = name
    this.age = age
}
Foo.prototype.getName = function () {
    return this.name }
// class 形式
class Foo {
   
    name = ''
    age = null 
    constructor(name, age) {
   
        this.name = name
        this.age = age
    } 
    getName() {
    return this.name }
}

const f = new Foo('小明', 22)
const name = f.getName()
console.log(name, 'name') // 小明 name
  • 创建一个空对象 obj,继承构造函数的原型
  • 执行构造函数(将 obj 作为 this)
  • 返回obj
function customNew<T>(constructor: Function, ...args: any[]): T {
   
    // 创建一个空对象,继承constructor的原型
    const obj = Object.create(constructor.prototype)
    // 将obj作为this,执行constructor,传入参数
    constructor.apply(obj, args)
    // 返回obj
    return obj 
}
// const f = new Foo('小明')
const f = customNew<Foo>(Foo, '小明')

chatGPT

  • 创建一个新对象,其原型指向构造函数的原型
  • 使用apply方法调用构造函数,并将新创建的对象作为this上下文
  • 检查构造函数的返回值

Object.create和{}有什么区别

  • {}创建空对象,原型指向 Object.prototype
  • Object.create 创建空对象,原型指向传入的参数
const obj1 = {
   
  • 28
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值