- call、apply、bind
- fn.call(obj, 10, 20, 30)
- fn.apply(obj, [10, 20, 30])
- fn.bind(obj)
- 三个函数都可以改变this的指向
- call和apply会立即执行,但是bind返回的只是改变了this后的函数,需要调用才能执行
- call和apply传参方式不同,call的性能比apply相对好一些
console.time('A')
...
console.timeEnd('A')
console.profile()
- 箭头函数与普通函数的区别
- 箭头函数语法比普通函数简单
- 箭头函数没有自己的this,箭头函数的this从属于所属上下文(使用call、apply等任何方式都无法改变箭头函数的this)
- 箭头函数没有arguments
- 箭头函数不能被new执行(箭头函数没有this,也没有prototype)
- 实现string的replace方法
(function () {
function myReplace(strOrReg, strOrFun) {
let newStr = this
let reg = strOrReg
if (typeof strOrReg === 'string') {
reg = new RegExp(strOrReg)
}
let rs = this.match(reg)
if (rs) {
if (typeof b === 'string') {
if (rs.length === 1) {
newStr = newStr.slice(0, rs['index']) + strOrFun + newStr.slice(rs['index'] + strOrReg.length)
} else {
for (let i = 0; i < rs.length; i++) {
let index = newStr.indexOf(rs[i])
newStr = newStr.slice(0, index) + strOrFun + newStr.slice(index + rs[i].length)
}
}
} else {
if (rs.length === 1) {
newStr = newStr.slice(0, rs['index']) + strOrFun(rs[0], rs[0].length, this) + newStr.slice(rs['index'] +
strOrReg.length)
} else {
for (let i = 0; i < rs.length; i++) {
let index = newStr.indexOf(rs[i])
newStr = newStr.slice(0, index) + strOrFun(rs[0], index, this) + newStr.slice(index + rs[i].length)
}
}
}
}
return newStr
}
String.prototype.myReplace = myReplace
})()
- 打印结果
var a = {}, b = '123', c = 123
a[b] = 'b'
a[c] = 'c'
console.log(a[b])
var a = {}, b = Symbol('123'), c = Symbol('123')
a[b] = 'b'
a[c] = 'c'
console.log(a[b])
var a = {},
b = {
key: '123'
},
c = {
key: '456'
}
a[b] = 'b'
a[c] = 'c'
console.log(a[b])
var b = 10;
(function b() {
b = 20
console.log('1', b)
})()
console.log('2', b)
let obj = {
2: 3,
3: 4,
length: 2,
push: Array.prototype.push
}
obj.push(1)
obj.push(2)
console.log(obj)
- $attr('xxx', 'yyy'),获得所有属性xxx值为yyy的标签
function $attr(property, value) {
let elements = document.getElementsByTagName('*'),
arr = []
elements = Array.from(elements)
elements.forEach(item => {
let itemValue = item.getAttribute(property)
if (property === 'class') {
new RegExp("\\b" + value + "\\b").test(itemValue) ? arr.push('item') : null
return
}
if (itemValue === value) {
arr.push(item)
}
})
return arr
}
let arr = $attr('class', 'box')
console.log(arr)
- 将数组扁平化
arr = arr.flat(Infinity)
arr = arr.toString().split(',').map(item => +item)
arr = Array.from(new set(arr)).sort((a, b) => a-b)
arr = [...new set(arr)].sort((a, b) => a-b)
arr = JSON.stringify(arr).replace(/(\[|\])/g, '').split('').map(item => +item)
while(arr.some(item => Array.isArray(item))){
arr = [].concat(...arr)
}
(function () {
function myFlat() {
let res = [],
_this = this
let fn = arr => {
for (let i = 0; i < arr.length; i++) {
let item = arr[i]
if (Array.isArray(item)) {
fn(item)
continue
}
res.push(item)
}
}
fn(_this)
return res
}
Array.prototype.myFlat = myFlat
})()
let arr = [1, [3, [4, [5, 6]]]]
console.log(arr.myFlat())
- == 如果类型不一样,先转为相同的数据类型再比较
- {} == {} false
- null == undefined true
- null === undefined false
- NaN == NaN false NaN和谁都不相等
- [12] == "12" true
if (a == 1 && a == 2 && a == 3) {
console.log(1)
}