Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override a function in another javascript file?

I have a JavaScript file, Mybasefile.js, which has the function Mybasefunction(). I want to override this function in another JavaScript file. When the function is called in a button click, I want the original Mybasefunction() to be executed along with some other code. How can I do this?

like image 216
Jishnu A P Avatar asked Jan 27 '11 09:01

Jishnu A P


People also ask

Does JavaScript have overriding?

Unlike the other programming languages, JavaScript Does not support Function Overloading.

How do I override a JavaScript function?

To override a method, we give it the same name and parameters as the method in the superclass. We can override receiveDamage() from the superclass by coding the same method in the subclass.

Which keyword is used to over ride the functions already existing in JavaScript?

TypeScript in version 4.3 introduced the override keyword which allows you to explicitly state that you intend to override a method from a base class.

Can I use a JavaScript function from another file?

Since ECMAScript 6 (or ES6) you can use the export or import statement in a JavaScript file to export or import variables, functions, classes or any other entity to/from other JS files.


1 Answers

place this code in the override file. Make sure the override file is included after the orginal one.

var orig_Mybasefunction = window.Mybasefunction;
window.Mybasefunction = function(){
    orig_Mybasefunction();
    ...
}
like image 152
Horia Dragomir Avatar answered Oct 19 '22 12:10

Horia Dragomir