手写 indexOf
function () {
function myIndexOf(a) {
let lena = a.length
y = this.length
flag = -1
if (lena > y) return -1
for (var i = 0; i <= y - lena; i++) {
if (this.substr(i, lena) === a) {
flag = i
break
}
}
return flag
}
String.prototype.myIndexOf = myIndexOf
}()
let demo = 'dwanlghMappaw'
let str = 'h'
console.log(demo.myIndexOf(str));
手写 instanceof
var arr = []
console.log(arr);
function instance_of(example, classFunc) {
let classFuncPrototype = classFunc.prototype
proto = Object.getPrototypeOf(example)
while (true) {
if (proto === null) {
return false
}
if (proto === classFuncPrototype) {
return true
}
proto = Object.getPrototypeOf(proto)
}
}
console.log(instance_of(arr, Array));
console.log(instance_of(arr, Object));
console.log(instance_of(arr, RegExp));
手写call方法
Function.prototype._call = function (context) {
if (typeof this != 'function') {
return
}
that = context || window
that.fn = this
const local = [...arguments].slice(1)
let result = that.fn(...local)
delete that.fn
return result
}
let obj = { a: 1 }
function fns(a, b) {
console.log(a, b);
console.log(this)
}
fns._call(obj, 23, 555)
手写apply
Function.prototype.myApply = function (context) {
if (typeof this !== 'function') {
return
}
that = context || window
that.fn = this
let result
if (arguments[1]) {
result = that.fn(...arguments[1])
} else {
result = that.fn()
}
return result
}
let obj = {
a: 1
}
function fn(...val) {
console.log(this)
console.log(...val)
}
fn.apply(obj, [1, 4, 5, 6, 7, 9])
手写 bind
Function.prototype._bind = function (context) {
if (typeof this != 'function') {
throw new TypeError('Error')
}
const args = [...arguments].slice(1)
fn = this
return function Fn() {
return fn.apply(this instanceof Fn ? new fn(...arguments) : context, args.concat(...arguments))
}
}
let obj = {
a: 1
}
function fn(...val) {
console.log(this)
console.log(...val)
}
fn._bind(obj, 231, 31242344, 432)()
简易版Promise.all
function PromiseAll(promiseArray) {
return new Promise((resolve, reject) => {
if (!Array.isArray(promiseArray)) {
return reject(new Error('传入的参数不是数组!'))
}
const res = []
let counter = 0
for (let i = 0; i < promiseArray.length; i++) {
Promise.resolve(promiseArray[i]).then(value => {
counter++
res[i] = value
if (counter === promiseArray.length) {
resolve(res)
}
}).catch(e => reject(e))
}
})
}
const s1 = new Promise((res, rej) => {
setTimeout(() => {
res('p1')
}, 1000)
})
const s2 = new Promise((res, rej) => {
setTimeout(() => {
res('p2')
}, 2000)
})
const s3 = new Promise((res, rej) => {
setTimeout(() => {
res('p3')
}, 3000)
})
const test = PromiseAll([s1,s2, s3])
.then(res => console.log(res))
.catch(e => console.log(e))
console.log(test);
数字千位分割
const format = (n) => {
let num = n.toString() // 拿到传进来的 number 数字 进行 toString
let len = num.length // 在拿到字符串的长度
// 当传进来的结果小于 3 也就是 千位还把结果返回出去 小于3 不足以分割
if (len < 3) {
return num
} else {
let render = len % 3 //传入 number 的长度 是否能被 3 整除
console.log(render)
/*
match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。
var str = '123123000'
str.match(/\w{3}/g).join(',') // 123,123,000
*/
if (render > 0) { // 说明不是3的整数倍
return num.slice(0, render) + ',' + num.slice(render, len).match(/\d{3}/g).join(',')
} else {
return num.slice(0, len).match(/\d{3}/g).join(',')
}
}
}
let str = format(298000)
console.log(str)
防抖
<body>
防抖: <input id="input" type="text">
</body>
<script>
input.addEventListener('input', function (e) {
val(e.target.value)
})
function fn(time, fun) {
let flag
return function (value) {
clearTimeout(flag)
flag = setTimeout(() => {
fun(value)
}, time)
}
}
let val = fn(1000, function (val) {
console.log(val)
})
</script>
节流
<body>
<button id="button">手在快1秒执行一次</button>
</body>
<script>
function throttle(fun, time) {
let flag = 0
return function () {
let now = +new Date().valueOf()
if (now - flag >= time) {
fun.apply(this, arguments)
flag = now
}
}
}
button.onclick = throttle((e) => {
console.log(e)
}, 1000)
</script>