1

1.正则将手机号中间四位替换为*

var phone = '12332112345'

phone = phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');

console.log(phone);  //123****2345

2.实现一个栈的先进后出

var Stack = function () {
    const stack = [];
    return {
        set: function (val) {
            stack.push(val)
        },
        get: function () {
            return stack.pop()
        }
    }
}
var stack = new Stack()
stack.set(1)
stack.set(2)
console.log(stack.get())
console.log(stack.get())

3.字符串连续去重

String.prototype.dedupe = function () {
    let str = this,
        len = str.length,
        result = ''
    for (let i = 0; i < len; i++) {
        if (str[0] == str[1]) {
            str = str.slice(1);
        } else {
            result += str[0];
            str = str.slice(1);
        }
    }
    return result;
}
console.log("abcdaaaaaaadbc".dedupe())

4.实现一个reduce

    Array.prototype.newReduce = function (fn, initValue) {
    let arr = this
    let num = initValue || arr[0]
    for (let i = initValue ? 0 : 1; i < arr.length; i++) {
        num = fn(num, arr[i], i, arr)
    }
    return num
    }
    console.log([1, 1, 1, 1].newReduce((a, b) => a + b)) //4
    console.log([1, 1, 1, 1].newReduce((a, b) => a + b), 10) //4 10

5.//手动实现一个bind

    Function.prototype.bind = function(obj){
    let _this = this;
    let arg1 = Array.prototype.slice.call(arguments,1);
    let bindFn = function (){
        let arg2 = Array.prototype.slice.call(arguments);
        return _this.apply(this instanceof bindFn?this:obj,arg1.concat(arg2))
    }
    let Fn = function(){};
    Fn.prototype = this.prototype;
    bindFn.prototype = bindFn;
    return bindFn
    }

6.实现一个instanceOf

    function newInstanceof(left,right){
    if(typeof left !== 'object' || left == null) return false
    let proto = Object.getPrototypeOf(left)
    while(proto){
        if(proto == right.prototype){
            return true
        }
        proto = Object.getPrototypeOf(left)
    }
    return false;
    }
    console.log(newInstanceof(1,Number))    

7.//深度遍历 深拷贝

    function cloneDeep(source){
    let result;
    if(typeof source === 'object'){
        if(Array.isArray(source)) {//先判断是不是数组,是的话直接循环取值
            source.map(item=>{
                result.push(item)
            })
        }else if(source ==null) {
            result = null
        }else if(source.constructor == RegExp){
            result = source
        }else{
            result = {}
            for(key in source){
                result[key] = cloneDeep(source[key])
            }
        }
    }else{
        result = source;
    }
    return result;
    }

8.//已知有2个栈,有pop,push,getSize接口,请用这2个栈实现1个队列,包含dequeue和enqueue接口。

    let left =[],right=[];
    function enqueue(node){
        left.push(node)
    }
    function dequeue(){
    let t = [];
    //如果在left栈push到right栈之前,right栈中有元素,先要将right中元素倒出来放到t中,
    //把left栈中元素全部倒入空的right栈中后,再把t中的元素倒回right栈中
    if(left.length>0){
        while(right.length>0){
            t.push(right.pop())
        }
    }
    while(left.length>0){
        right.push(left.pop())
    }
    if(t.length>0){
        while(t.length>0){
            right.push(t.pop())
        }
    }
    return right.pop()
    }

一只特立独行的猫
172 声望6 粉丝

前端资深菜鸟一枚,只想在前端的路上更进一步。