JavaScript原型功能覆盖

时间:2018-07-25 21:15:54

标签: javascript

我正在使用Github的开源Able Player UI是非常基本的,我想知道扩展播放器并覆盖诸如

之类的功能的最佳方法
AblePlayer.prototype.getSvgData = function(button) {...
AblePlayer.prototype.setButtonImages = function() {...

以便我可以使用自己的图标等。

不装饰原始ableplayer.js

1 个答案:

答案 0 :(得分:1)

最好根据MDN扩展具有原型继承最佳实践的AblePlayer

(function(){
    const AblePlayer = window.AblePlayer;

    function MyAblePlayer(...args) {
        AblePlayer.call(this, ...args);
    }

    MyAblePlayer.prototype = Object.create(AblePlayer.prototype);
    MyAblePlayer.prototype.constructor = MyAblePlayer;

    /* @override method*/
    MyAblePlayer.prototype.getSvgData = function (...args) {
        // if you need to call super do:
        const superResult = AblePlayer.prototype.getSvgData.call(this, ...args);

        // add your code here

        return superResult; // or return some other result
    }

    // Copy static properties
    Object.assign(MyAblePlayer, AblePlayer)

    // Finally override global reference
    window.AblePlayer = MyAblePlayer;
})();