js原生方法的重写

讲干货,不啰嗦,通过prototype可以获取到JavaScript的原型对象,进而可以在对象原型上添加新的属性和方法,当该方法与原方法名称一样时会覆盖原方法既:重写,当不一样时既:添加

如:实现数组Array的push方法的重写

Array.prototype.push = function() {
            for( let i = 0 ; i < arguments.length ; i++){
                this[this.length] = arguments[i] ;//arguments为传参数组列表
            }
            return this.length;
        }

能力有限,水平一般,错误之处,欢迎指正,感谢关注和评论!

原文地址:https://www.cnblogs.com/wwlstc/p/11225619.html