偽代碼如下:var Constructor = function(arr) { // 在這里對(duì)傳入的數(shù)組進(jìn)行處理}var $ = function(arr) { return new Constructor(arr);}Constructor.prototype = Object.create(Array.prototype);Constructor.prototype.add = function(item) { ... }我預(yù)期的結(jié)果是:$(arr) 作為 Constructor 的實(shí)例可以調(diào)用自定義的 add() 方法,也可以調(diào)用 Array.prototype 上的數(shù)組的原生方法。主要是不希望直接在 Array.prototype 上定義 add() 方法。arr = [1, 2, 3] 本來(lái)是 Array 的實(shí)例,但是我希望在 arr 和 Array 中間添加一個(gè)構(gòu)造函數(shù),即 arr 的原型鏈為 Constructor.prototype ==> Array.prototype我準(zhǔn)備用上面?zhèn)未a的方式實(shí)現(xiàn),但是 Constructor 內(nèi)部不知道怎么處理,或者這種思路根本就是錯(cuò)的,各位大神不要被偽代碼影響。請(qǐng)問(wèn)可以實(shí)現(xiàn)這種功能嗎?如果可以,怎么實(shí)現(xiàn)?
實(shí)現(xiàn)一個(gè)可以繼承 Array 的構(gòu)造函數(shù)可行嗎?
qq_遁去的一_1
2018-12-27 22:15:40