编写高质量代码 - 面试题

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]

手写一个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 = {
   }
obj1.__proto__ === Object.protoTYpe
const obj2 = Object.create({
   x
  • 28
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
级开发面试一般包括更加细致和级的技术问,涉及到开发者在实际项目中的运用和解决问的能力。以下是一个可能的回答: 在级开发面试中,面试官可能会问到关于具体开发框架或者技术栈的问。例如,他们可能会要求我解释MVC框架以及我在实际项目中的应用经验。我会回答MVC的全称是Model-View-Controller,它是一种软件开发架构模式,将应用程序分为三个主要部分:模型,视图和控制器。模型负责处理数据逻辑,视图负责显示数据和界面,控制器处理用户的请求并负责协调模型和视图之间的交互。对于MVC的应用经验,我可以分享我在过去项目中使用MVC框架的经历,以及如何通过它来组织和管理代码的优势。 除了框架和技术栈问级开发面试还可能涉及到代码质量保证和性能优化方面的问。例如,面试官可能会问我在实际项目中如何确保代码质量和可维护性。我会谈到我在代码开发过程中遵循SOLID原则,编写可读性强的代码,并使用单元测试和集成测试来确保代码的正确性和可靠性。另外,我还会提到我在代码评审中的经验,以及如何利用代码静态分析工具和自动化测试工具来帮助检测潜在的问。 性能优化也是一个重要的话。如果被问到如何提应用程序的性能,我会谈到我在过去项目中使用的一些策略,如对数据库进行索引优化,减少网络传输量,优化算法和数据结构等。 总的来说,级开发面试旨在测试开发者在实际项目中的运用和解决问的能力。通过提出关于框架、技术栈、代码质量保证和性能优化等方面的问面试官可以更全面地了解开发者的能力和经验,并决定是否适合担任级开发职位。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值