Overriding existing functions

Overriding existing functions

Backport allows you to overwrite existing functions to fix bugs and increase/extend functionality.

Availability

Backport.js v0.1 and above.

Syntax, Parameters, Return values

Exactly the same as Adding missing functions.

Examples

The key difference between adding a missing function and overriding an existing function is that in the case of overrides you'll almost certainly want to access native API features, in other words you'll probably still want to call the original function at some point via backport.global or functionName.native.

// Warzone 3.1 Beta 1 had a bug in pickStructLocation()
// see http://forums.wz2100.net/viewtopic.php?f=35&t=8694 for details
// Let's fix that bug...
 
(function() {
 
  // We can work out if this bug is present by checking whether removeReticuleButton() is present
  // removeReticuleButton() was added to the JS API in the same version that the pickStructLocation() bug was fixed

  if (!backport.global.removeReticuleButton) { // the pickStructLocation() bug is present
 
    // define a new function that calls the original function then fixes it's return value
    backport("pickStructLocation",function(droid,structureType,x,y) {
      // apply the original (native) function
      var pos = pickStructLocation.native.apply(null,arguments);
      // the bug effected everything except cyborg factories
      if (structureType != "A0CyborgFactory") {
        pos.x += 1;
        pos.y += 1;
      }
      // return correct position back to calling script
      return pos;
    });
 
  }
 
})();
 
// thereafter, scripts can safely use pickStructLocation() without having to worry about offset bugs :)

You can get the pickStructLocation bugfix in a ready-made backport script: pickStructLocation.js (smile)

See also