Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript override methods

Let's say you have the below code:

function A() {     function modify() {        x = 300;        y = 400;     }      var c = new C(); }  function B() {     function modify(){        x = 3000;        y = 4000;     }      var c = new C(); }  C = function () {    var x = 10;    var y = 20;     function modify() {       x = 30;       y = 40;    };     modify();    alert("The sum is: " + (x+y)); } 

Now the question is, if there is any way in which I can override the method modify from C with the methods that are in A and B. In Java you would use the super-keyword, but how can you achieve something like this in JavaScript?

like image 914
Daniel Nastase Avatar asked Jul 30 '11 19:07

Daniel Nastase


People also ask

Can you override methods in JavaScript?

Introduction. It is true that JavaScript supports overriding, not overloading. When you define multiple functions that have the same name, the last one defined will override all the previously defined ones and every time when you invoke a function, the last defined one will get executed.

Can we override a method?

A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.

How do I override a node JS method?

To use a query string value to override the method, specify the query string key as a string argument to the methodOverride function. To then make the call, send a POST request to a URL with the overridden method as the value of that query string key.

What does override method do?

Instance Methods The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.


Video Answer


1 Answers

Edit: It's now six years since the original answer was written and a lot has changed!

  • If you're using a newer version of JavaScript, possibly compiled with a tool like Babel, you can use real classes.
  • If you're using the class-like component constructors provided by Angular or React, you'll want to look in the docs for that framework.
  • If you're using ES5 and making "fake" classes by hand using prototypes, the answer below is still as right as it ever was.

Good luck!


JavaScript inheritance looks a bit different from Java. Here is how the native JavaScript object system looks:

// Create a class function Vehicle(color){   this.color = color; }  // Add an instance method Vehicle.prototype.go = function(){   return "Underway in " + this.color; }  // Add a second class function Car(color){   this.color = color; }  // And declare it is a subclass of the first Car.prototype = new Vehicle();  // Override the instance method Car.prototype.go = function(){   return Vehicle.prototype.go.call(this) + " car" }  // Create some instances and see the overridden behavior. var v = new Vehicle("blue"); v.go() // "Underway in blue"  var c = new Car("red"); c.go() // "Underway in red car" 

Unfortunately this is a bit ugly and it does not include a very nice way to "super": you have to manually specify which parent classes' method you want to call. As a result, there are a variety of tools to make creating classes nicer. Try looking at Prototype.js, Backbone.js, or a similar library that includes a nicer syntax for doing OOP in js.

like image 133
Adam Avatar answered Oct 06 '22 00:10

Adam