js面试题2024

  • 1.js的数据类型

boolean number string null undefined bigint symbol object
按存储方式分,前面七种为基本数据类型,存储在栈上,object是引用数据类型,存储在堆上,在栈中存储指针
按es标准分,bigint 和symbol是es6新增的数据类型,bigint存储大整数,symbol解决全局属性名冲突的问题

  • 2.js数据类型检测的方式
typeof 2 //number
typeof true //boolean
typeof 'srt' //string
typeof undefined //undefined
typeof null //object
typeof 1n //bigint
typeof Symbol() //symbol
typeof {} //object
typeof [] //object
typeof function(){} //function

Object.prototype.toString().call()

([] instanceof Array)
(function(){} instanceof Function)
({} instanceof Object)
//instanceof只能用于对象,返回布尔值
    (2).constructor===Number//true
    (true).constructor===Boolean//true

  • 3.判断数组类型的方式有那些
//1.通过原型判断
const a=[]
const b={}
a instanceof Array
Array.prototype.isPrototypeOf(a)
a.__proto__===Array.prototype
//2.通过object.prototype.tostring.call()
const a=[]
Object.prototype.toString().call(a)
//3.es6的array.isarray()
Array.isArray(a)
  • 4.null和undefined的区别

undefinde代表为定义,变量声明了但未初始化是未定义
null代表空对象,一般用作某些对象变量的初始化值
undefined==void 0
typeof null=object null的地址是0和对象的地址相同

    1. 0.1+0.2!==0.3
// 方法一:放大10倍之后相加在缩小十倍
//方法二:封装浮点数相等的函数
function feg(a,b){
    return Math.abs(a-b)<Number.EPSILON
}
feg(0.1+0.2,0.3)
  • 6.空类型
[]==false//true
Boolean([])//true
Number([])//0
  • 7.包装类型
const a='abc'
a.length//3
a.toUpperCase()//'ABC'
const c=Object('abc')
const cc=Object('abc').valueOf()

  • 8.new做了什么工作

1.创建了一个新的空对象object
2.将新空对象与构造函数通过原型链连接起来
3.将构造函数中的this绑定到新建的object上并设置为新建对象result
4.返回类型判断

function MyNew(fn,...args){
    const obj={}
    obj.__proto__=fn.prototype
    let result=fn.apply(obj,args)
    return result instanceof Object?result:obj
}
function Person(name,age){
    this.name=name
    this.age=age
}
Person.prototype.sayHello=function(){
    console.log(this.name)
}
const person=MyNew(Person,'test',20)
person.sayHello()
  • 9.继承
//1.原型链继承
    function Sup(){
        this.prop='sup'
    }
    Sup.prototype.show=function(){}
    function Sub(){
        this.prop='sub'
    }
    Sub.prototype=new Sup()
    Sub.prototype.constructor=Sub
    Sub.prototype.show=function(){}
//2.构造函数继承
    function Person(name,age){
        this.name=name
        this.age=age
    }
    function Student(name,age,price){
        Person.call(this,name,age)
        this.price=price
    }
//3.原型链加构造函数
    function Person(name,age){
        this.name=name
        this.age=age
    }
    Person.prototype.show=function(){}
    function Student(name,age,price){
        Person.call(this,name,age)
        this.price=price
    }
    Student.prototype=new Person()
    Student.prototype.constructor=Person
    Student.prototype.show=function(){}
    //4.class extends
    class Animal{
        constructor(kind){
            this.kind=kind
        }
    }
    class Cat extends Animal{
        constructor(kind) {
            super.constructor(kind);
        }
    }
  • 10.深拷贝
    function deep(p,c){
        let c = c||{}
        for(let i in p){
            if(typeof p[i]==='object'){
                c[i]=p[i].constructor=='Array'?[]:{}
                deep(p[i],c[i])
            }else{
                c[i]=p[i]
            }
        }
        return c
    }

相关推荐

  1. js面试2024

    2024-07-12 23:14:02       20 阅读
  2. js面试

    2024-07-12 23:14:02       41 阅读
  3. js面试

    2024-07-12 23:14:02       30 阅读
  4. 面试】Node.js高频面试

    2024-07-12 23:14:02       24 阅读
  5. Docker面试2024

    2024-07-12 23:14:02       40 阅读
  6. node.js 面试 1

    2024-07-12 23:14:02       26 阅读
  7. 2024年Python面试

    2024-07-12 23:14:02       54 阅读
  8. 【SpringBoot】springboot面试2024

    2024-07-12 23:14:02       47 阅读
  9. Linux篇面试 2024

    2024-07-12 23:14:02       35 阅读
  10. Spring篇面试 2024

    2024-07-12 23:14:02       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 23:14:02       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 23:14:02       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 23:14:02       62 阅读
  4. Python语言-面向对象

    2024-07-12 23:14:02       72 阅读

热门阅读

  1. ArkTS学习笔记_自定义组件

    2024-07-12 23:14:02       24 阅读
  2. Oracle的wrap工具怎么用

    2024-07-12 23:14:02       24 阅读
  3. 昇思25天学习打卡营第18天 | LSTM+CRF序列标注

    2024-07-12 23:14:02       20 阅读
  4. Memcached介绍和详解

    2024-07-12 23:14:02       24 阅读
  5. 论文阅读:A Survey on Evaluation of Large Language Models

    2024-07-12 23:14:02       26 阅读
  6. c#中将数据库中的文件导出为csv、xml文件的demo

    2024-07-12 23:14:02       22 阅读
  7. ceph gps backfill_toofull

    2024-07-12 23:14:02       20 阅读
  8. [NeetCode 150] Products of Array Discluding Self

    2024-07-12 23:14:02       25 阅读
  9. NCNN源码学习(1):Mat详解

    2024-07-12 23:14:02       19 阅读