小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

javascript-不共享OLOO繼承中的對(duì)象屬性

 印度阿三17 2019-11-19

我不確定使用OLOO繼承鏈中的每個(gè)對(duì)象都是獨(dú)立的對(duì)象屬性的最佳方法.

檢查此小提琴或考慮以下代碼:
http:///HB7LU/19413/

Parent = {
    array: [],

    add: function(element) {
        this.array.push(element   this.array.length.toString());
        return this;
    },

    getAll: function() {
        return this.array;
    }
};

Child = Object.create(Parent, {
    removeAllButOne: { value: function() {
        this.array.splice(1);
        return this;
    }}
});

foo = Object.create(Parent);
foo.add('foo');

bar = Object.create(Child);
bar.add('bar');

在小提琴中,單擊foo或bar文本將調(diào)用foo.add(…)或bar.add(…)函數(shù),將一個(gè)元素添加到對(duì)象數(shù)組,從而導(dǎo)致一個(gè)額外的< p>.標(biāo)簽在輸出中.
結(jié)果不是我想要的. foo和bar共享同一數(shù)組.但是很容易理解會(huì)發(fā)生什么,如果我們查詢對(duì)象繼承,我們會(huì)看到以下內(nèi)容:
inheritance

好的,那我該怎么辦?我想到了兩種選擇:

選項(xiàng)1)
http:///HB7LU/19419/

Parent = function() {
    return {
        array: [],

        add: function(element) {
            this.array.push(element   this.array.length.toString());
            return this;
        },

        getAll: function() {
            return this.array;
        }
    };
};

Child = Object.create(Parent(), {
    removeAllButOne: { value: function() {
        this.array.splice(1);
        return this;
    }}
});

foo = Object.create(Parent());
foo.add('foo');

bar = Object.create(Child);
bar.add('bar');

這將創(chuàng)建一個(gè)新的Parent對(duì)象,每次創(chuàng)建Parent對(duì)象或從(新的)Parent對(duì)象“繼承”子對(duì)象時(shí),都會(huì)創(chuàng)建Parent對(duì)象的所有功能.雖然這解決了我遇到的問(wèn)題,但始終為每個(gè)子類型對(duì)象重復(fù)創(chuàng)建相同的函數(shù)似乎是一個(gè)壞主意.

選項(xiàng)2)
http:///HB7LU/19420/

Parent = Object.create({
    add: function(element) {
        this.array.push(element   this.array.length.toString());
        return this;
    },

    getAll: function() {
        return this.array;
    }
}, {
    ctor: { value: function(someArgs) {
        this.array = [];
        // maybe use someArgs
        return this;
    }}
});

Child = Object.create(Parent, {
    removeAllButOne: { value: function() {
        this.array.splice(1);
        return this;
    }}
});

foo = Object.create(Parent).ctor();
foo.add('foo');

bar = Object.create(Child).ctor();
bar.add('bar');

這似乎也解決了問(wèn)題,但避免了Parent對(duì)象及其功能的重新生成.那么這是走的路嗎?如果我在繼承鏈中有多個(gè)孩子也都有私有財(cái)產(chǎn)怎么辦?

像這樣嗎

Child = Object.create(Parent, {
    ctor: { value: function(someArgs) {
        this.__proto__.ctor(someArgs);
        this.otherPrivate = {};
        // maybe use someArgs
        return this;
    }},

    removeAllButOne: { value: function() {
        this.array.splice(1);
        return this;
    }}
});

孩子會(huì)用自己的功能遮蓋父ctor …但是在其ctor函數(shù)中,他們可以調(diào)用父ctor而不破壞功能.

意見(jiàn)和建議深表感謝,謝謝!

解決方法:

最簡(jiǎn)單的方法是使用構(gòu)造函數(shù),因此始終在實(shí)例上將數(shù)組創(chuàng)建為自己的屬性

// define Parent
function Parent() {
    this.array = []; // array will be an instance property
}
Parent.prototype = {}; // inherit all the goodies from Object.prototype
Object.assign(Parent.prototype, { // using `Object.assign` for shorthand
    add: function (element) {
        this.array.push(element   this.array.length.toString());
        return this;
    },
    getAll: function () {
        return this.array;
    }
});

// define Child
function Child() {
    Parent.apply(this); // apply Parent constructor to the instance
}
Child.prototype = Object.create(Parent.prototype); // inherit Parent's prototype chain
Object.assign(Child.prototype, {
    removeAllButOne: function () {
        this.array.splice(1);
        return this;
    }
});

現(xiàn)在有

var a = new Child(),
    b = new Child();
a.array === b.array; // false

您也可以使用ES 6的類來(lái)編寫(xiě)此代碼,但這只是我上面編寫(xiě)的語(yǔ)法糖,將產(chǎn)生相同的結(jié)構(gòu).

來(lái)源:https://www./content-1-565651.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多