Search code examples
javascriptjqueryfunction

How to remove/delete a JavaScript function()?


How would I go about removing a function if a certain browser is detected ? Here is the function and the detection:

function parallaxIt(e, target, movement) {
  var $this = $("#app");
  var relX = e.pageX - $this.offset().left;
  var relY = e.pageY - $this.offset().top;

  TweenMax.to(target, 1, {
    x: (relX - $this.width() / 2) / $this.width() * movement,
    y: (relY - $this.height() / 2) / $this.height() * movement,
    z: 0.01,
    rotation:0.01
  });
}


var version = detectIE();
if (version === false) {
  //Do Nothing
} else if (version >= 12) {
    //Remove Function
} else {
    $("#iefix").attr({href : "/static/css/app-iefix.css"});
    //Remove Function
}

So when any IE is detected I want to remove the function parallaxIt() or break it so It doesn't work any ideas? Kind Regards

(the detect part of the code is obviously a small snippet of the full code so it's easier for you folks to read instead of going through the full code)


Solution

  • At least for me in Chrome v71 (Windows 10), deleting the function does not do anything (it keeps existing). But you can re-assign it to become a no-op function:

    function parallaxIT() {
      console.log("inside parallaxIT");
    }
    
    // delete
    delete window.parallaxIT;
    console.log("parallaxIT was 'deleted', does it still show output:");
    parallaxIT();
    
    console.log("=====");
    
    // re-assign
    window.parallaxIT = function() {}    // no-op function
    console.log("parallaxIT was re-assigned, does it still show output:");
    parallaxIT();
    
    console.log("=====");