如何";“保存”;Javascript函数?

如何";“保存”;Javascript函数?,javascript,function,Javascript,Function,我有一个带有数组参数的函数,其中包含不同的值和回调 此函数的目的是修改其中一个回调(成功回调),如果它存在于选项数组中。修改是在执行保存的函数之前添加一些操作 我知道好词不是“拯救”,但很难解释。因此,我让你评论下面的代码,这可能会更清楚 function myFunction(options){ if (options.success) { // I would like to save the function without execute it t

我有一个带有数组参数的函数,其中包含不同的值和回调

此函数的目的是修改其中一个回调(成功回调),如果它存在于选项数组中。修改是在执行保存的函数之前添加一些操作

我知道好词不是“拯救”,但很难解释。因此,我让你评论下面的代码,这可能会更清楚

function myFunction(options){
    if (options.success) {
        // I would like to save the function without execute it
        temporaryVariable = options.success;
    }

    // Then I want to modify the function into the array parameter in order to add some actions before to execute the previous callback
    options.success = function () {
        // Some actions
        console.log('Some actions');
        // Then execute the previous callback I've "save"
        temporaryVariable();
    };
    return options;
}
有什么建议吗?

你差一点就搞定了:

function myFunction(options){
    var temporaryVariable = null;
    if (options.success) {
        // I would like to save the function without execute it
        temporaryVariable = options.success;
    }

    // Then I want to modify the function into the array parameter in order to add some actions before to execute the previous callback
    options.success = function () {
        // Some actions
        console.log('Some actions');
        // Then execute the previous callback I've "save"
        if (temporaryVariable) {
            temporaryVariable();
        }
    };
    return options;
}
你差一点就搞定了:

function myFunction(options){
    var temporaryVariable = null;
    if (options.success) {
        // I would like to save the function without execute it
        temporaryVariable = options.success;
    }

    // Then I want to modify the function into the array parameter in order to add some actions before to execute the previous callback
    options.success = function () {
        // Some actions
        console.log('Some actions');
        // Then execute the previous callback I've "save"
        if (temporaryVariable) {
            temporaryVariable();
        }
    };
    return options;
}

听起来您只是想创建一个对象,将其用作属性包,然后调用“当前”包:

function myObject(options)
{
   this.options = options;
}
myObject.prototype.changeOption = function(optName, optValue)
{
   this.options[optName] = optValue;
}
myObject.prototype.performAction = function()
{
    // do something with options;
    return this.options;
}

var myObjectInstance = new myObject({'success':true});
myObjectInstance.changeOption('target','someOtherThing');
myObjectInstance.send();

听起来您只是想创建一个对象,将其用作属性包,然后调用“当前”包:

function myObject(options)
{
   this.options = options;
}
myObject.prototype.changeOption = function(optName, optValue)
{
   this.options[optName] = optValue;
}
myObject.prototype.performAction = function()
{
    // do something with options;
    return this.options;
}

var myObjectInstance = new myObject({'success':true});
myObjectInstance.changeOption('target','someOtherThing');
myObjectInstance.send();
你可以用

function myFunction(options){
    var temporaryVariable = options.success;

    // Then I want to modify the function into the array parameter in order to add some actions before to execute the previous callback
    options.success = function () {
        // Some actions
        console.log('Some actions');
        // Then execute the previous callback I've "save"
        if (typeof temporaryVariable === 'function') {
            temporaryVariable();
        }
    };
    return options;
}
一些注意事项:

  • 始终记住使用
    var
    声明新变量
  • 如果您不知道是否能够调用
    temporaryVariable
    ,最好检查
    typeof temporaryVariable=='function'
    ,而不是只检查
    temporaryVariable
    是否正确
  • 由于您需要检查是否可以调用
    temporaryVariable
    ,因此在创建
    temporaryVariable
    时不需要检查
    选项。成功
    • 您可以使用

      function myFunction(options){
          var temporaryVariable = options.success;
      
          // Then I want to modify the function into the array parameter in order to add some actions before to execute the previous callback
          options.success = function () {
              // Some actions
              console.log('Some actions');
              // Then execute the previous callback I've "save"
              if (typeof temporaryVariable === 'function') {
                  temporaryVariable();
              }
          };
          return options;
      }
      
      一些注意事项:

      • 始终记住使用
        var
        声明新变量
      • 如果您不知道是否能够调用
        temporaryVariable
        ,最好检查
        typeof temporaryVariable=='function'
        ,而不是只检查
        temporaryVariable
        是否正确
      • 由于您需要检查是否可以调用
        temporaryVariable
        ,因此在创建
        temporaryVariable
        时不需要检查
        选项。成功

      注意:无需将
      临时变量设置为
      null
      。您可以只使用
      var-temporaryVariable准确,我已经接近答案了。我认为
      var
      指令没有用。谢谢Success函数过去有一个数据参数(实际上是一个ajax回调)。您知道是否也可以访问此参数吗?这是可能的。通过在函数中声明int或使用arguments数组。注意:无需将
      temporaryVariable
      设置为
      null
      。您可以只使用
      var-temporaryVariable准确,我已经接近答案了。我认为
      var
      指令没有用。谢谢Success函数过去有一个数据参数(实际上是一个ajax回调)。您知道是否也可以访问此参数吗?这是可能的。可以在函数中声明int,也可以使用arguments数组。谢谢,它工作得很好!你能解释一下为什么测试
      typeof temporaryVariable==='function'
      更好吗?Success函数通常有一个数据参数(实际上它是一个ajax回调)。您知道是否也可以访问此参数吗?@Fractalise最好检查它是否是函数,因为如果您只检查它是否是真正的变量并尝试调用它,但它不是函数,则会抛出错误。@Fractalise关于数据参数,这取决于具体情况。我应该看看完整的代码,看看它是在哪里声明的,或者你怎么能通过它,等等。谢谢,它工作得很好!你能解释一下为什么测试
      typeof temporaryVariable==='function'
      更好吗?Success函数通常有一个数据参数(实际上它是一个ajax回调)。您知道是否也可以访问此参数吗?@Fractalise最好检查它是否是函数,因为如果您只检查它是否是真正的变量并尝试调用它,但它不是函数,则会抛出错误。@Fractalise关于数据参数,这取决于具体情况。我应该看看完整的代码,看看它是在哪里声明的,或者如何传递它,等等。