js基础和数据类型面试题

153 阅读7分钟
  1. new操作符的实现原理
    创建新对象
    将对象的__proto__指向构造函数的prototype
    将构造函数的this执行新对象执行(继承属性)
    返回新对象

set和数组区别
set值唯一,数组值不唯一
set只有键值,数组可以键值对

  1. map和Object的区别 map键值可以是任意值,object只能时string
    map有迭代器对象可以直接遍历
    map个数可以通过size方法获得,object不行
    map是有序的,object是无序的

  2. JavaScript有哪些内置对象
    内置全局属性null nan undefined
    内置全局构造函数 objct date function
    内置函数 Parseint parseFloat
    内置数学处理对象 math

  3. JavaScript脚本延迟加载的方式有哪些?
    defer 并行加载,等文档解析完再解析
    async 异步加载,加载完立即解析,若文档此时没解析完,会阻塞
    sttimeout设计定时器延时加载
    js放最后

  4. JavaScript 类数组对象的定义? 一组具有length和若干索引的对象,和数组相似,但不能调用数组的方法
    类数组转数组 array.form,展开运算符,slice.call,splice.call,concat.apply

  5. 数组有哪些原生方法? foreach map reduce shfit pop push resevet sort

  6. 为什么函数的 arguments 参数是类数组而不是数组?如何遍历类数组?
    因为不能调用数组的方法
    遍历类数组将类数组转成数组,再遍历
    通过call调用数组foreach

  7. 什么是 DOM 和 BOM? domhtml文档对象,主要处理了网页效果和交互
    bom浏览器对象,处理了浏览器的交互
    bom包括dom

  8. JavaScript为什么要进行变量提升,它导致了什么问题?
    因为能提高性能,提高代码容错性
    导致了变量未声明就能使用,声明的变量会变成全局变量,变量会声明在函数顶部

ES6模块与CommonJS模块有什么异同?
es6是对引入文件的引用,com是文件的拷贝
es6是编译时加载,com解析时加载
es6是异步,com同步
相同都能操作引入的文件,都能对引入的对象进行赋值

  1. use strict是什么意思 ? 使用它区别是什么?
    严格模式,使代码在更加严格的情况下运行,消除js语法不合理处,减少错误代码,减少怪异行为,提高编译器效率,增快 运行速度 禁止this指向全局对象
    对象不能有重名的属性

如何判断一个对象是否属于某个类?
通过constractor 是否指向构造函数,可被修改
通过instanceof 判断是否在原型链上
通过object.ptototype.tostring.call 打印对象的calss属性

强类型语言和弱类型语言的区别 是否需要明确类型

  1. for...in和for...of的区别 for in es3,遍历键名 ,会遍历其原型链,一般用于遍历对象
    for of es6 遍历键值 ,可以用于遍历对象,数组,类数组,map,set

如何使用for...of遍历对象
只要含有迭代器接口,都可以遍历

数组遍历方法有哪些 for foreach filter map reduce

ajax、axios、fetch的区别
ajax 是原生xhr对象,为mvc模式准备,配置和调用比较混乱
fetch 基于ptomise,脱离了xhr对象,api更丰富,语法更简洁;
axios 基于promise封装的http对象,支持promiseapi,能拦截请求和响应,还能取消请求,还能自动转 换json格式,预防xsrf攻击

**

**数据类型# ****
JavaScript有哪些数据类型,它们的区别?
null undefined boolean string number symbol object bigint
分为原始数据类型(存在栈)和引用数据类型(存在堆)(arrry,object,函数)
bigint出现是因为number有最大值限制

数据类型检测的方式有哪些
type of
console.log(typeof 2); // number
console.log(typeof true); // boolean
console.log(typeof 'str'); // string
console.log(typeof []); // object
console.log(typeof function(){}); // function
console.log(typeof {}); // object
console.log(typeof undefined); // undefined
console.log(typeof null); // object
对象,数组,null不能检测 其他都行

instanceof
可以正确的判断对象的类型,其原理是判断是否能在原型链上找到该类型的原型
console.log(2 instanceof Number); // false
console.log(true instanceof Boolean); // false
console.log('str' instanceof String); // false
console.log([] instanceof Array); // true
console.log(function(){} instanceof Function); // true
console.log({} instanceof Object); true
只能判断引用数据类型

constructor
原理,通过对象的constructor 是否指向构造函数
console.log((2).constructor === Number); // true
console.log((true).constructor === Boolean); // true
console.log(('str').constructor === String); // true
console.log(([]).constructor === Array); // true
console.log((function() {}).constructor === Function); // true
console.log(({}).constructor === Object); // true

Object.prototype.toString.call() (原理 获取对象class的值,class表明了该对象的类型)
var a = Object.prototype.toString;
console.log(a.call(2)); [object Number]
console.log(a.call(true));[object Boolean]
console.log(a.call('str'));[object String]
console.log(a.call([]));[object Array]
console.log(a.call(function(){}));[object Function]
console.log(a.call({}));[object Object]
console.log(a.call(undefined));[object Undefined]
console.log(a.call(null));[object Null]
为什么不直接用 ,因为对象都重写了tostring方法 ,因此需要call调用未重写的tostring方法

判断数组的方式有哪些
array.isarray
instanceof
construotr Array
object.ptototype.tostring.call

null和undefined区别
null 表示声明为了一个变量值为null,定义空对象
undefined 相当于声明了但是为赋值

typeof null 的结果是什么,为什么?
结果是object

intanceof 操作符的实现原理及实现
通过取得对象的__proto__,判断是否在构造函数中能否找到
function myInstance(left,right){
let leftProto = left.ptoto
left rightProto = right.ptorotype
//递归判断原型链
while(true){
if rightProto retrun false //如果原型链上为空了,直接返回true
if rightProto= leftProto retrun true
rightProto = Object.getProtogetTypeof(rightProto)
//通过getProtogetTypeof获取对象上原型 }

}

  1. 为什么0.1+0.2 ! == 0.3,如何让其相等
    因为进制问题,可以通过四舍五入tofixd

8.如何获取安全的 undefined 值?
通过void 0 ,直接使用undefined当变量来赋值,会影响判断

  1. typeof NaN 的结果是什么?
    number nan是数字计算出错时返回的结果,not a number
    nan!=nan 一个非自反的特殊值

  2. isNaN 和 Number.isNaN 函数的区别?
    isnan 会直接进行数字转换,若转换失败,则直接返回true(会影响判断nan结果,如一个非数值字符串)
    number 会进行类型判断是不是数字类型,再判断是不是nan,不会进行数据类型转换,结果更精确

  3. == 操作符的强制类型转换规则?
    先判断是否是相同数据类型,如果是直接判断,不是的话进行数据类型转换
    转换规则
    number == str 转number
    null== undefined true
    boolean == any Boolean转number
    obeject =基本类型,obect转基本类型,转换失败return false,转换成功再根据转换值进行判断

68747470733a2f2f63646e2e6e6c61726b2e636f6d2f79757175652f302f323032312f706e672f313530303630342f313631353437353231373138302d65616265383036302d613636612d343235642d616434632d3337633363613633386136382e706e67.png

  1. 其他值到字符串的转换规则?
    null 'null'
    true false 'true' 'false'
    undefined 'undefined'
    1 '1' 极大和极小值会通知指数形式 1e+36
    symbol 直接转换只允许强制转换,不允许隐式转换
    普通对象 会调用object.ptoto.tostring 返回内部的class值 【object Object】,如果重写了tosring会直接调用tostring方法

  2. 其他值到数字值的转换规则?
    true fasle 1 0
    null 0
    '111' 111
    undefined nan
    'xxx' nan
    symbol 报错
    对象 先转成基本类型,再转数字

15.Object.is() 与比较操作符 “===”、“==” 的区别?
== 数据类型相同直接判断,数据类型不同进行数据类型转换
=== 数据类型相同直接判断,数据类型不同直接fasle
object.is 在=== 基础上判断了nan = nan;+0 !=-0

  1. JavaScript 中如何进行隐式类型转换? + 优先转换为字符串,但凡两边有一个字符串,都会转字符串,两边为数值就为数值运算

- * / 将双方都转为数字进行运算

== 尽量将双方转成数值进行运算

> 和< 如果双方是字符串,进行字符编码顺序判断,其他都转为数字判断

  1. object.assign和扩展运算法是深拷贝还是浅拷贝,两者区别
    (都是浅拷贝,区别拓展的每一项都会被拷贝到新对象里,而assign 是将目标对象合并到源对象) 都是浅拷贝
    let outObj = {
    inObj: {a: 1, b: 2}
    }
    let newObj = {...outObj}
    newObj.inObj.a = 2
    console.log(outObj) // {inObj: {a: 2, b: 2}}

Object.assign():
let outObj = {
inObj: {a: 1, b: 2}
}
let newObj = Object.assign({}, outObj)
newObj.inObj.a = 2
console.log(outObj) // {inObj: {a: 2, b: 2}}