简体   繁体   English

Javascript创建函数以在其他函数之间共享变量

[英]Javascript creating function to share variables between other functions

I have a couple click functions with jQuery that share the same variables, so I created a function to return those variables. 我有几个与jQuery共享相同变量的click函数,所以我创建了一个返回这些变量的函数。

While this works, I'm wondering whether programmatically speaking this is the right or most efficient way to do this: 虽然这有效,但我想知道是否以编程方式说这是正确或最有效的方法:

function clickVars($me){
    var $curStep = $('.cust-step-cur'),
        $nextStep = $curStep.next('.cust-step'),
        nextStepLen = $nextStep.length,
        $list = $('.cust-list'),
        $btnCheck = $('.cust-btn.checklist'),
        hasChecklist = $me.hasClass('checklist');

    return {
        curStep: $curStep,
        nextStep: $nextStep,
        nextStepLen: nextStepLen,
        list: $list,
        btnCheck: $btnCheck,
        hasChecklist: hasChecklist
    };
}

// Checklist Click
$('.option-list a').on('click',function(){
    var $me = $(this),
        myVars = clickVars($me);        

    currentStepOut(myVars.curStep);

    myVars.curStep.removeClass('cust-step-cur');

    currentStepIn(myVars.nextStep, myVars.list, myVars.btnCheck);
});

// Navigation
$('.cust-btn').on('click',function(){
    if(animReady === false) 
        return false;

    var $me = $(this),
        myVars = clickVars($me);


    if(myVars.hasChecklist && myVars.list.hasClass('cust-step-cur'))
        return false;

    currentStepOut(myVars.curStep);

    myVars.curStep.removeClass('cust-step-cur');

    if(myVars.nextStepLen === 0 || $me.hasClass('checklist')) {
        myVars.nextStep = myVars.list;
    }

    animReady = false;

    currentStepIn(myVars.nextStep, myVars.list, myVars.btnCheck);
});

Is this a standard way of generated shared variables between multiple functions? 这是在多个函数之间生成共享变量的标准方法吗?

In AS3 it's good practice to do: 在AS3中,最好这样做:

 // Variable definitions
 var enabled:Boolean = false;

 public function myFunction(){
      enabled = true;
 }

So in JavaScript I've been doing: 所以在JavaScript中我一直在做:

 // Variable defintions
 var a,b,c,d,e = 0;

 function alterVariables(){
      a = 1;
      b = 2;
 }

You have to understand you are not sharing variables between functions. 您必须了解您不是在函数之间共享变量。 Moreover, each time you click those elements, clickVars function is called again and again, even if you click only one element multiple times. 此外,每次单击这些元素时,即使您多次单击一个元素,也会一次又一次地调用clickVars函数。 So this code is very bad expirience. 所以这段代码非常糟糕。 Check this: 检查一下:

// Defined ones 
var nodes = {
    $elements       : $('.elements'),
    $otherElements  : $('.otherElements'),
}

// In case you have multiple .selector elements in your DOM
$('.selector').each(function() {

   // Defined ones for each element
   var $element = $(this), isList = $element.hasClass('.list');

   $element.bind('click', function(){
     nodes.$elements.addClass('clicked');
   });
});


$('.anotherSelector').each(function() {

   // Yep, here is some duplicate code. But there won't be any
   // performance improvement if you create special method for
   // such small piece of code
   var $element = $(this), isList = $element.hasClass('.list');

   $element.bind('click', function(){
     nodes.$elements.addClass('clicked');
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Javascript中的函数之间共享变量 - Share variables among functions in Javascript 如何在一个是模块的 Javascript 文件和另一个不是模块的 Javascript 文件之间共享函数和变量 - How to share functions and variables between one Javascript file that IS a module, and another Javascript file that is NOT a module Vue中如何在函数之间共享变量 - How to share variables between functions in Vue 在 Javascript 中的函数之间传递变量? - Pass variables between functions in Javascript? javascript中的函数之间共享变量 - Sharing variables between functions in javascript 在JavaScript中的函数之间传递变量 - Passing variables between functions in Javascript 嵌套函数可以使用在父函数中其他函数中设置了值的变量吗? (JavaScript) - Can nested functions use variables with values set in other functions within the parent function? (Javascript) 在Java函数原型中创建函数 - Creating functions in function prototypes for Javascript AngularJS:如何与其他控制器共享范围函数和变量 - AngularJS: How to share scope functions and variables with other controllers 在JavaScript中将函数分配给变量并将变量分配给function(?) - assigning function to variables and assigning variables to functions(?) in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM