JavaScript面试篇

typeof可以判断几种类型

typeof 1 // "number"
typeof "a" // "string"
typeof true // "boolead"
typeof undefined // undefined
typeof null //"object"
let a = Symbol(1)
typeof a // "symbol"

如何判断是一个数组

  1. 利用构造函数instanceofconstructor
let a = []
a instanceof Array // true
a.constructor === Array // true
  1. ES6新增Array.isArray()
let a = []
Array.isArray(a) // true
  1. 使用Object.prototype.toString.call()
let a =[]
Object.prototype.toString.call(a) // '[object Array]'

如何判断是空对象

  1. 使用JSON.stringify() === '{}'
let a = {}
JSON.stringfy(a) === '{}'
  1. ES6的Object.keys()
  • Object.keys()返回自身的可枚举属性
let a = {}
Object.keys(a).length === 0
  1. 使用for in得到自身和原型的可枚举属性,再使用hasOwnPrototype()获取自身的可枚举属性
let obj = {}
for(key in obj){
	if(Object.prototype.hasOwnProperty.call(obj,key)){
		return false
	}
	return true
}
  1. 使用Object.getOwnPropertyNames获取自身的所有属性(可枚举和不可枚举)
    Object.getOwnPropertyNames(obj).length === 0

区分Object.prototype.toString()Object.toString()

  • ObjectObject.prototype均有toString()方法,Object.toString()返回函数而Object.prototype.toString()返回类型
Object.toString()//function Object(){native code}
Object.prototype.toString()//'[object Object]'
Object.toString.call(Array)//function Array(){native code}
Object.prototype.toString.call(Array)//'[object Function]'
  • 为什么不能用Array.prototype.toString.call([])
    原因在于Function,Array,Date都继承自Object,因此他们的toString()来自于Object.toString()
let a = [1,2,3]
Object.prototype.toString.call(a) // "[object Array]"
Object.prototype.toString.call(Array) // "[object Function]"
Object.toString(a) //"1,2,3"
a.toString() // "1,2,3"
Array.prototype.toString.call(a) // "1,2,3"

正则判断电话号码

// inputValue
let validTel = /^(86|0|17951)?(13[0-9]|14[57]|15[0-9]|17[0678]|18[0-9])[0-9]{8}&/
validTel.test(inputValue)

正则将首字母大写

var replaceAll(str){
	str = str.toLowerCase()
	return str.replace(/\b(\w)|\s(\w)/g,function(up) { up.toUpperCase()})
}
replaceAll("this is oo")//"This Is Oo"

正则清除空格trim()

function mytrim(str){
	return str.replace(/^\s|\s+$/g,'')
}

如何渲染后台的html片段

innerTextinnerHTML的区别

  1. innerHTML返回的是html标签和内容
  2. innerText只返回内容

谈谈fetch

如何冻结对象

  • 不可扩展对象Object.preventExtensions()
  • 密封对象Object.seal()
  • 冻结对象Object.freeze()
对象
不可拓展对象×
密闭对象××
冻结对象×××

如何找到内存泄露的位置

Number()的存储空间?如何做到大数相乘?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值