如何删除/删除JavaScript function()?

时间:2019-01-02 12:49:11

标签: javascript jquery function

如果检测到某个浏览器,我将如何删除功能? 这里是功能和检测:

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
}

因此,当检测到任何IE时,我想删除功能parallaxIt()或将其破坏,以至于它不起作用吗? 亲切的问候

(代码的检测部分显然是完整代码的一小段,因此您阅读起来比阅读完整代码更容易)

1 个答案:

答案 0 :(得分:2)

至少对于我来说,在Chrome v71(Windows 10)中,删除该功能不会执行任何操作(此功能会一直存在)。但是您可以重新分配以使其成为无操作功能:

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("=====");