js,将var解析回其父级

时间:2012-03-27 12:36:12

标签: javascript jquery variables

我有两个函数,父函数是__triggerAction,而子函数是__triggerNormal,其中父$ current正在被更新,如何解析新的

$current = $index

从__triggerNormal回到__triggerAction?

  _triggerAction = function(el, click, fx) {
    var $current, $link;
    $link = el.find('.tab-menu').find('li');
    $current = 0;
    if (click) {
      log('Trigger by click');
      return $link.click(function() {
        return __triggerNormal(this, el, $current, fx);
      });
    } else {
      log('Trigger by hover');
      return $link.hover(function() {
        return __triggerNormal(this, el, $current, fx);
      });
    }
  };

 __triggerNormal = function(thisEl, el, $current, fx) {
    var $index;
    if (!($(thisEl).hasClass("active"))) {
      log('current slide ' + $current);
      log($index = $(thisEl).index());
      __addRemoveClass(thisEl);
      __fxAction(el, fx, $current, $index);
      return $current = $index;
    }
};

1 个答案:

答案 0 :(得分:0)

而不是

return __triggerNormal(this, el, $current, fx);

DO

$current = __triggerNormal(this, el, $current, fx);
return $current;
函数之间不共享

$current,因此在__triggerNormal内,

return $current = $index;

只更新局部变量。你可以写

return $index;

顺便说一句,事件处理程序的返回值仅用于确定是否阻止默认操作并停止传播。在您的情况下,似乎没有必要return $current