Javascript变量范围问题

时间:2012-12-20 20:53:10

标签: javascript function variables scope

我有一个变量mutedUser,我想继续使用另一个函数。我在click事件之外持久存在变量时遇到了一些麻烦。拥有它的最佳方法是什么,以便“返回mutedUser”将根据if语句的条件保持“静音”字符串添加?谢谢!

* console.log是我检查持久性停止的地方

this.isUserMuted = function isUserMuted(payload) {
  var mutedUser = '';
  // If mute button is clicked place them into muted users list
  // check for duplicates in list
  $("#messages-wrapper").off('click', '.message button.muteButton');
  $("#messages-wrapper").on('click', '.message button.muteButton', function(e) {

      $('#unMute').show();

      //create userId reference variable
      var chatUserID = parseInt($(this).parent().parent().attr("data-type"));

      //store userId in muted user object
      mutedUsers[chatUserID] = {};
      mutedUsers[chatUserID].id = chatUserID;
      mutedUsers[chatUserID].muted = true;

      if (mutedUsers[chatUserID] !== null && mutedUsers[chatUserID].id === payload.a) {
          console.log("user is now muted");
          mutedUser += ' muted';
          console.log(mutedUser + 1);
      }
      console.log(mutedUser + 2);
  });
  return mutedUser;
};

3 个答案:

答案 0 :(得分:5)

如果我理解你要做的事情(通过查看代码),这将是最好的方法:

// If mute button is clicked place them into muted users list
// check for duplicates in list
$("#messages-wrapper").off('click', '.message button.muteButton');
$("#messages-wrapper").on('click', '.message button.muteButton', function(e) {
    $('#unMute').show();

    //create userId reference variable
    var chatUserID = parseInt($(this).parent().parent().attr("data-type"));

    //store userId in muted user object
    mutedUsers[chatUserID] = {};
    mutedUsers[chatUserID].id = chatUserID;
    mutedUsers[chatUserID].muted = true;
});

this.isUserMuted = function isUserMuted(payload) {
  var mutedUser = '';

  if (mutedUsers[payload.a] !== null) {
      mutedUser += ' muted';
  }

  return mutedUser;
};

如果提供的用户在该数组中,代码将保留mutedUsersisUserMuted函数检查的数组。在您提供的代码中,每次调用isUserMuted函数时都会附加一个新的事件处理程序。

isUserMuted功能甚至可以缩短为:

this.isUserMuted = function isUserMuted(payload) {
  return mutedUsers[payload.a] !== null ? ' muted' : '';
};

答案 1 :(得分:3)

修改

抱歉,我的错误。另一种方法是传入该变量,即

this.isUserMuted = function isUserMuted(payload, isMuted) {
  isMuted = '';
  // If mute button is clicked place them into muted users list
  // check for duplicates in list
  $("#messages-wrapper").off('click', '.message button.muteButton');
  $("#messages-wrapper").on('click', '.message button.muteButton', function(e) {

      $('#unMute').show();

      //create userId reference variable
      var chatUserID = parseInt($(this).parent().parent().attr("data-type"));

      //store userId in muted user object
      mutedUsers[chatUserID] = {};
      mutedUsers[chatUserID].id = chatUserID;
      mutedUsers[chatUserID].muted = true;

      if (mutedUsers[chatUserID] !== null && mutedUsers[chatUserID].id === payload.a) {
          console.log("user is now muted");
          isMuted += ' muted';
          console.log(mutedUser + 1);
      }
      console.log(mutedUser + 2);
  });
  return isMuted;
};

答案 2 :(得分:2)

你做不到。如果从函数返回一个字符串,它将始终按值传递,即复制;它的价值不会再改变了。您需要返回一个可以访问局部变量当前值的函数,或者一个具有正在更改的属性的对象。

由于您似乎已经拥有了一个对象,因此选项#2适合这里:

function User() { // or whatever you have
    …

    var user = this;
    // If mute button is clicked place them into muted users list
    // check for duplicates in list
    $("#messages-wrapper").on('click', '.message button.muteButton', function(e) {

        $('#unMute').show();

        //store userId in muted user object
        mutedUsers[user.id] = user;
        user.muted = true;
    });
    this.muted = false;
    this.isUserMuted = function() {
        return this.muted ? ' muted' : '';
    }
}