typeof
可以判断几种类型
typeof 1
typeof "a"
typeof true
typeof undefined
typeof null
let a = Symbol(1)
typeof a
如何判断是一个数组
- 利用构造函数
instanceof
和 constructor
let a = []
a instanceof Array
a.constructor === Array
- ES6新增
Array.isArray()
let a = []
Array.isArray(a)
- 使用
Object.prototype.toString.call()
let a =[]
Object.prototype.toString.call(a)
如何判断是空对象
- 使用
JSON.stringify() === '{}'
let a = {}
JSON.stringfy(a) === '{}'
- ES6的
Object.keys()
let a = {}
Object.keys(a).length === 0
- 使用
for in
得到自身和原型的可枚举属性,再使用hasOwnPrototype()
获取自身的可枚举属性
let obj = {}
for(key in obj){
if(Object.prototype.hasOwnProperty.call(obj,key)){
return false
}
return true
}
- 使用
Object.getOwnPropertyNames
获取自身的所有属性(可枚举和不可枚举)
Object.getOwnPropertyNames(obj).length === 0
区分Object.prototype.toString()
和Object.toString()
Object
和Object.prototype
均有toString()
方法,Object.toString()
返回函数而Object.prototype.toString()
返回类型
Object.toString()
Object.prototype.toString()
Object.toString.call(Array)
Object.prototype.toString.call(Array)
- 为什么不能用
Array.prototype.toString.call([])
原因在于Function
,Array
,Date
都继承自Object
,因此他们的toString()
来自于Object.toString()
let a = [1,2,3]
Object.prototype.toString.call(a)
Object.prototype.toString.call(Array)
Object.toString(a)
a.toString()
Array.prototype.toString.call(a)
正则判断电话号码
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")
正则清除空格trim()
function mytrim(str){
return str.replace(/^\s|\s+$/g,'')
}
如何渲染后台的html
片段
innerText
和innerHTML
的区别
innerHTML
返回的是html
标签和内容innerText
只返回内容
谈谈fetch
如何冻结对象
- 不可扩展对象
Object.preventExtensions()
- 密封对象
Object.seal()
- 冻结对象
Object.freeze()
对象 | 增 | 删 | 改 |
---|
不可拓展对象 | × | √ | √ |
密闭对象 | × | × | √ |
冻结对象 | × | × | × |
如何找到内存泄露的位置
Number()的存储空间?如何做到大数相乘?