js原生方法的重寫

講乾貨,不囉嗦,經過prototype能夠獲取到JavaScript的原型對象,進而能夠在對象原型上添加新的屬性和方法,當該方法與原方法名稱同樣時會覆蓋原方法既:重寫,當不同時既:添加數組

 

如:實現數組Array的push方法的重寫this

Array.prototype.push = function() {
            for( let i = 0 ; i < arguments.length ; i++){
                this[this.length] = arguments[i] ;//arguments爲傳參數組列表
            }
            return this.length;
        }

 

能力有限,水平通常,錯誤之處,歡迎指正,感謝關注和評論!spa

相關文章
相關標籤/搜索