将 eval 替换为在变量命名空间内或变量命名空间外调用函数

Replace eval to call a function with in a variable namespace or outside the variable namespace

我有一个变量 namespace。从那个命名空间我需要调用一个函数。我在变量namespace外写了一个函数。如果我使用 window[functionName] 则该函数正在调用和执行。

假设如果我将函数放在变量命名空间中,则不会调用该函数。如果我使用 eval 那么它正在工作。是否有 eval 的任何替换以在变量中调用函数 namespace.window[funcitonName].call(args) 仅当该函数在全局时才执行。

我的要求是这样的:

var xxx = xxx || function($) {

  function onVehicleChange(..., functionName, ....) {
    window[functionName](this, args);
  }

  function maruthiVehicle(args) {

  }
}

这里我动态获取函数名称。假设我这里的functionNamemaruthiVehicle;我在这里硬编码该功能。

window[functionName] 现在变成 window[maruthiVehicle] 没有调用,因为我的 function(maruthiVehicle) 在变量 namespace(XXX) 中。假设如果我将我的 function(maruthiVehicle) 放在这个变量 namespace(xxx) 之外,那么它就可以工作。

所以 window[functionName] 只在 global 范围内工作。我尝试使用 eval。当我将函数放在变量 namepsace 的外部或内部时,eval 工作正常;

我不想使用 eval。我想更换这个 eval.

eval(functionName + "(args)"); 无论我的函数放置在变量内部还是变量命名空间外部,我的函数调用都应该被触发。

我相信你可以像下面这样构造代码:

/* 
 * Global varibale - a namespace / place holder to keep the entities related to the application
 * This goes into the global variable - `window`
 */
vehicleNamespace = window.vehicleNamespace || {};

/* 
 *  Add functionality into that object
 */
vehicleNamespace = (function() {

  // Create a `local` function.
  var maruthiVehicle = function() {
    console.log("This is Maruthi");
  };
  
  // Return the object(s) that should be public in `vehicleNamespace`
  return {
    maruthiVehicle: maruthiVehicle
  }
})();

// call the function
window.onload = function() {
  vehicleNamespace.maruthiVehicle();
};

为了调用函数dynamically,您可以根据需要使用call or bind

有关其用法,请参阅下面的代码段:

/* 
 * Global varibale - a namespace / place holder to keep the entities related to the application
 * This goes into the global variable - `window`
 */
vehicleNamespace = window.vehicleNamespace || {};

/* 
 *  Add functionality into that object
 */
vehicleNamespace = (function() {

  var maruthiVehicle = function(input) {
    console.log(input);
  };

  var onVehicleChange = function(functionName, output) {
    // Note: You could also add more validations here to check if it is a `function`
    if (typeof functionName == 'function') {
      functionName.call(this, output);
    }
  }

  // Return the object(s) that should be public in `vehicleNamespace`
  return {
    maruthiVehicle: maruthiVehicle,
    onVehicleChange: onVehicleChange
  }
})();

// call the function
window.onload = function() {
  vehicleNamespace.maruthiVehicle("This is Maruthi");
  vehicleNamespace.onVehicleChange(vehicleNamespace.maruthiVehicle, "Selected Maruthi");
};

function testGlobalNetworkChange(xyz, $) {
    console.log("selectoris " + this.id + ',' + "mode is "
                + xyz+ ',' + "jQuery instance is " + $);
}

    var netowrk= network|| (function($) {
    'use strict';
    return {
        onNetworkChange: onNetworkChange,
        testnetworkChange: testnetworkChange
    };

    function testnetworkChange(mode, $) {
        console.log("AttributeSelector is " + this.id + ','
                    + "mode is "+ mode + ',' + "jQuery instance is " + $);
    }

    function solveFunction(functionName, context) {
        var namespaces = functionName.split(".");
        var func = namespaces.pop();
        for (var i = 0; i < namespaces.length; i++) {
            context = context[namespaces[i]];
        }
        return context[func];
    }

    function OnNetworkChange(selector, functionName, xyz, $) {
        var func = solveFunction(functionName, window);
        $('#' + Selector).change(function(e) {
            func.apply(this, [xyz, $]);
        });
    }
})(jQuery);

// 调用您的函数进行测试。 network.onNetWorkChange(参数);