简体   繁体   中英

JavaScript prototype functions overriding

I am using open source Able Player from Github The UI is very basic and I was wondering the best way to extend the player and override functions like

AblePlayer.prototype.getSvgData = function(button) {...
AblePlayer.prototype.setButtonImages = function() {...

So that I can use my own icons etc.

without motifying the original ableplayer.js

It is best to extend AblePlayer with prototypical inheritance best practices according to MDN

(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;
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM