Search code examples
javascriptoverridingprototypejs

Override js function which has arguments


I want to overwrite js method which has arguments.

Please check original js file : bundle.js

Product.Bundle = Class.create();
Product.Bundle.prototype = {
    reloadPrice: function() {

         -----  Default Code  -----

    },
    selectionPrice: function(optionId, selectionId) {

         -----  Default Code  -----
    },
}

I have to overwrite both methods in my js file. I can successfully overwrite reloadPrice method in my js but can not overwrite selectionPrice method.

Please check my js file code in which reloadPrice method is overwrite successfully

<script>
    Product.Bundle.prototype.reloadPrice =
        Product.Bundle.prototype.reloadPrice.wrap(function(parentMethod) {
            -----  Default Code  ------
        });


        // Doing same thnig for SelectionPrice method but not worked :
        Product.Bundle.prototype.selectionPrice =
            Product.Bundle.prototype.selectionPrice.wrap(function(parentMethod) {
            -----  Default Code  -----
        });
</script>

Where I am wrong.Please help me.


Solution

  • When you have to overwrite method which has an arguments then you can overwrite that like below :

    <script>
     Product.Bundle.prototype.selectionPrice =
         Product.Bundle.prototype.selectionPrice.wrap(function(parentMethod,optionId, selectionId)  {
            -----  Default Code  -----
        });
    </script>