【问题标题】:replace javascript function with a new function [duplicate]用新函数替换javascript函数[重复]
【发布时间】:2017-02-22 21:11:22
【问题描述】:

在我们的订购系统中,有一个我根本无法访问的嵌入式功能。方便的是其中存在拼写错误,因此当用户单击某些内容时会出现一个弹出窗口并且其中存在语法问题。

我有没有办法替换该文本或将函数替换为具有所有相同代码但拼写正确的新函数?

这是我需要编辑的功能。注意确认说“您的选择”而不是“您的选择” 我宁愿替换整个东西,因为我可能想做一些其他的编辑,但现在我想修复那个拼写错误。

 function showProof()
{
    var blnSubmit = false;
    var strHid='';
    var strSave='';

  if (aryTemplateChoices.length == 0) 
  {
    blnSubmit = true;
  }
  else
  {
    for (var i=1; i<=aryTemplateChoices.length; i++)
    {
        strSave = aryTemplateChoices[i-1];
        if (strSave=='')strSave='0';
        if (document.getElementById('hidctrl'+ i))strHid = document.getElementById('hidctrl'+ i).value;
        if (strHid=='')strHid='0';

      if ((strSave != '0') && (strHid != strSave))
      {
        blnSubmit = true;
        break;
      }

    }
  }

  if (blnSubmit)
  {
    if (confirm('Your selection has changed, do you want to save?'))
    {
      document.getElementById('subtype').value = 'proof';
      document.getElementById('prevclick').value = '';
      document.getElementById('frm1').submit();
    }
  }

  canAddToCart();
  //<!--WRITE-->
  getQuantityPrice()
  //<!--/WRITE-->
    loadProof();

} 

【问题讨论】:

  • 只需在更接近您想要使用它的范围内重新定义函数(具有相同的名称和更正的代码)。
  • 为什么无法访问代码?
  • 取决于定义的位置以及引用/调用的方式。
  • 它是 .net 中的在线订购系统,代码位于我无法编辑的 asp 文件之一中。它可能已经在更新中修复,但使用我们的自定义编辑更新系统将花费大量时间。
  • @Radiusmg 我已经在下面发布了答案。

标签: javascript jquery


【解决方案1】:

原始函数在哪里以及如何访问它并不重要,只要在更接近使用它的范围内重新定义函数(具有相同的名称和更正的代码)即可。

这是一个例子:

function foo(){
  console.log("ORIGINAL foo says hello.");
}

function foo(){
  console.log("NEW foo says hello.");
}

// The second declaration is in the same scope as the first, but it comes after the first
// so it overwrites that declaration and the second one is the one that is used.
foo();

【讨论】:

  • 终于让它工作并标记为答案斯科特。主要障碍是找到我可以放置它的位置,以便它出现在代码中的原始位置之后。
猜你喜欢
  • 2020-11-08
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
  • 2013-05-08
  • 1970-01-01
相关资源
最近更新 更多