Parameters 通过一系列函数调用参数是一种不好的做法吗?

Parameters 通过一系列函数调用参数是一种不好的做法吗?,parameters,javascript,Parameters,Javascript,我正在构建一个相当大的JS应用程序,我想听听您对其中一些逻辑的看法。 我想知道通过一系列函数传递参数是否被认为是不好的做法,例如 function start(){ var param1 = 'me'; secondFunction(param1); } function secondFunction(param1){ //i dont want to user the param in this function $.ajax('url',data,succe

我正在构建一个相当大的JS应用程序,我想听听您对其中一些逻辑的看法。 我想知道通过一系列函数传递参数是否被认为是不好的做法,例如

function start(){
    var param1 = 'me';
    secondFunction(param1);
}

function secondFunction(param1){
    //i dont want to user the param in this function
    $.ajax('url',data,success(){
        third(param1);
    });
}

function third(param1){
    alert(param1);
}
我想另一种选择是使用全局变量,如下所示。但是在我的例子中,我已经有了大量的全局变量,在我看来,有些事情对于应用程序的全局工作来说还不够重要

var param1;

function start(){
    param1 = 'me';
    secondFunction();
}

function secondFunction(){
    //i dont want to user the param in this function
    $.ajax('url',data,success(){
        third();
    });
}

function third(){
    alert(param1);
}
那么,您认为通过一个以上的函数传递参数是可以的,还是我应该用另一种方式


感谢

事实上,这是一个很好的实践,因为它避免了任何全局状态(即,理想情况下,函数的行为只取决于其参数)

如果您有许多参数要以这种方式传递,我会将它们批处理到一个单独的对象(一个“环境”对象)中,但除此之外,这是完全好的


这样做给了你很大的灵活性——如果你想让一个函数在不同的数据上运行一次,你只需要传递不同的值,而不是改变全局状态,这也可能会影响其他一切(没有这样的全局副作用,使得函数的并行化非常容易,即使这对JavaScript来说可能并不重要)。

通过多个函数传递参数以使用全局函数要好得多


例如,它允许您多次启动流程,而不会弄乱全局状态。

我认为这两种样式都没有任何错误。这里的首选方法,真的


globals的一个缺点是,它们在应用程序的整个生命周期中都留在内存中。

传递参数是非常合理的,实际上,如果使用异步ajax调用,这是唯一可行的方法。您不能保证全局变量可能会被另一个对start()的调用覆盖在ajax调用第一次start()调用的回调之前。

javascript有一种特殊的方法来定义变量的作用域,在第一个示例中,您在start中定义了param1,因此在start函数的上下文中和上下文下调用的所有线程和函数都具有变量param1“inherit”以某种方式调用。因此,在start之外使用的所有语句都没有定义变量param1。因此,只有在需要在线程或调用的一系列函数中使用变量时,才需要使用此变量范围

var param1;

function start(){
    param1 = 'me';
    secondFunction();
}

function secondFunction(){
    //i dont want to user the param in this function
    $.ajax('url',data,success(){
        third();
    });
}

function third(){
    alert(param1);
}
第二个示例使用了一个全局变量范围,这意味着无论您调用了哪个线程,该变量都将与全局值一起使用,这仅适用于不在另一个上下文中再次定义param1的情况


因此,这取决于你要做什么,在上面的例子中,mosst效率是第二个

这是一个有点难回答的问题,因为它完全取决于手头的任务

将参数传递给每个函数的原因是,您可能希望将函数用于其他目的或以不同的顺序重用。在这种情况下,您肯定希望继续传递参数

如果后两个函数只供第一个函数使用,那么创建一个封装东西的函数/对象将是一个好方法

var ObjectName = {
  var param = null;

  function mainFunc(p) {
    pararm = p;
    func2();
  }

  function func2() {
    // Use "param"
    func3();
  }

  function func3() {
    // Use "param"
  }
}

// At this level of the code, the param variable is out of scope.

太多的全局变量是个坏主意:你会忘记什么服务于什么

函数中的参数太多是一个坏主意:它将变得混乱/难以回忆哪些参数与哪些函数匹配

你可以先重新考虑你的设计

按照亚历山大·盖斯勒(Alexander Gessler)的建议,按对象对它们进行分组是个好主意


另一个想法是为函数/变量创建不同的作用域…(不一定在对象中)函数中的函数。如果您愿意的话。

长话短说,我会坚持传递变量,以避免范围冲突并保持同时调用正常运行。但是,我会更改传递它们的格式:

因为JavaScript对对象非常灵活,我喜欢为可能需要多个参数的函数使用一个包罗万象的参数。这提高了调用函数时的可视性,使扩展函数以支持附加参数和可选参数变得容易,并且在您的情况下,使传递参数t变得容易o链中的附加函数。一个缺点是较长的函数调用,但我认为其优点非常值得。例如:

/**
 * ..
 *
 * params.test1 Test 1 does blah
 * params.test2 Test 2 does blah 2
 * params.test3 Test 3 does blah 3
 * params.test4 Test 4 does blah 4
 */
function test(params){
    //Initialize Parameters
    var test1 = params.test1;
    var test2 = params.test2;

    //function code
    ..
    test2(params);
    ..
}

/**
 * ..
 *
 * params.test1 Test 3 does blah 3
 * params.test2 Test 4 does blah 4
 */
function test2(params){
    var test3 = params.test3;
    var test4 = params.test4;

    //function code using test3 and test4
    ..
}

//Call the test function
test({test1: 'foo1', test2: 'foo2', test3: 'foo3', test4: 'foo4'});

像这样的东西对你有好处

var cakes = function() {
    // Sets a varable called param1, will use it in the prototype below
    this.param1 = 'i like cakes';
    // Calls the init prototype, setup below.
    this.init();
};

cakes.prototype = {
    init: function() {
        // this.param1 is set above.
        console.log(this.param1);
        $.ajax({
            // For example I'm just passing back the page which this javascript is in, so something returns
            url: window.location.href,
            // _this stores the full object, so you can get it in the callback below.
            _this: this,
            // success calls the ajaxCallback function, below
            success: this.ajaxCallback
        });
    },
    ajaxCallback: function() {
        // this here refers to the ajax call
        console.log(this);
        // this._this refers to the original object, captured above.
        console.log(this._this);
        // this._this.param1 refers to the text set above.
        console.log(this._this.param1);
    }
};

// Make sure you include this or it won't work
var x = new cakes();
// or you could just do this if you are only planning on using one instance of this object on a page.
// new cakes();

不要害怕使用私有变量,这里有一个。它可能不符合您的需要,但需要注意的是,您并不总是在全局变量和参数之间进行选择。两者之间有很多选择

var MyNamespace = function () {

    // private variable
    var myPrivateVariable = 'me';

    // private methods
    var secondFunction = function () { };

    // return an object, this becomes MyNamespace
    return {
        // expose a method called start
        start: function (arg1) {
            // take the arg and assign it to a private
            myPrivateVariable = arg1;

            // call a private variable, we can only call / access privates from within the returned object assigned to MyNamespace
            secondFunction();
        },
        // expose a method called third
        third: function () {
            // alert out our private variable
            alert(myPrivateVariable);
        }
    }; 

} ();  // here we're assigning the result of this function call to MyNamespace and closing around all the privates



// Usage:
MyNamespace.start('me');

MyNamespace.third();

这是不正确的。在异步ajax调用完成之前多次调用
start()
将覆盖以前对
start()
的调用中设置的
param1
。假设分配给
param1
的值实际上不是常量,这将扰乱以前对
start()的调用的状态
。在他的问题中,param1始终具有相同的值。不过,感谢您的意见。很可能他在实际代码中没有为param1赋值。如果是这种情况,使用全局变量将破坏他的程序。因此(以及其他各种原因)通常认为使用全局变量是不好的做法,应该避免使用它们。因此,这不仅仅是偏好或风格的问题,而是在可能破坏应用程序的代码和不会破坏应用程序的代码之间进行选择。如果有可行的替代方案,请不要使用全局变量。Instea在拥有大量全局变量的情况下,您可以创建一个名称空间(基本上是一个对象),并将变量分配给它。这样,您只有一个全局变量,并且仍然可以访问所有内容