简体   繁体   English

从函数内部更改全局变量,或实现相同的最佳实践

[英]Changing the global variable from inside a function, or best practice to achieve the same

I am trying to change the variable called projectCounter in a JQuery project so that I don't have to repeat buttons inside each div for each project. 我试图在JQuery项目中更改名为projectCountervariable ,这样我就不必为每个项目重复每个div内的按钮。 How can I make the change in one function accessible to all functions? 如何在所有功能都可以访问的功能中进行更改? This is what I have so far: 这是我到目前为止:

var projectCounter = 1;

$('a').click(function() {
  function setValue() {
    var projectCounter = projectCounter + 1;
    alert(window.projectCounter);
  }
});

I have made a JSfiddle also: http://jsfiddle.net/cPFRD/ 我也做了一个JSfiddle: http//jsfiddle.net/cPFRD/

Leave the first var and remove the one inside the function 保留第一个var并删除函数内的var

var projectCounter = 1;
$('a').click(function() {
  function setValue() {
    projectCounter = projectCounter + 1;
    alert(window.projectCounter);
  }
});

Your setValue() function never runs in the code you posted. 您的setValue()函数永远不会在您发布的代码中运行。 It will only be defined after a click (so it's not available before) and only after that click can you call/run it. 它只会在点击后定义(因此之前不可用),并且只有在点击之后才能调用/运行它。 Is that what you want? 那是你要的吗? If you want the projectCounter variable to add on click, remove the whole function setValue() leaving only its content. 如果要在单击时添加projectCounter变量,请删除整个function setValue() ,仅保留其内容。

when you run setValue() projectCounter will be 2, in this example. 当你运行setValue() projectCounter将是2,在这个例子中。 You can also use just projectCounter++; 你也可以只使用projectCounter++; to achieve same as projectCounter = projectCounter + 1; 实现与projectCounter = projectCounter + 1;

The following code will work even if div>a's are added after the javascript has been run. 即使在运行javascript后添加了div> a,以下代码也能正常工作。

> Example Fiddle < >示例小提琴<

JavaScript JavaScript的

var counter = 0;

$('.container').on('click', 'div a', function () {
  counter++;
  alert(counter);
});

HTML HTML

<div class="container">
  <div>
    <a>Increase Counter</a>
  </div>
</div>
var projectCounter = 1;

$('a').click(function() {
  setValue();//this actually calls the function and thus makes it happen
});

//defining/declaring a function, doesn't actually do anything until it is called
 function setValue() {
    projectCounter = projectCounter + 1;//no var keyword because we want to reference the existing variable, not declare a new one
    alert(window.projectCounter);
  }

This should address the problems with why your code is not working. 这应解决您的代码无法正常工作的问题。 I don't make any claims that this is best practice though. 我并不认为这是最佳做法。

   var projectCounter = 1;

   function setValue() {
     projectCounter +=  1;
     alert(window.projectCounter);
   }   

   $('a').click(function(e) {
      e.preventDefault();
      setValue();
   });

I think this is what you were trying to accomplish. 我想这就是你想要完成的事情。 now you can add other events to call setValue() . 现在您可以添加其他事件来调用setValue() i must say that you should be more specific with what a tag you're referrring to. 我必须说你应该更具体地说明你所引用a标签。

Your counter is in global scope, hence visible everywhere: 您的柜台处于全球范围,因此无处不在:

var projectCounter = 1;

$('a').click(function() {
  alert(projectCounter++);
});
var projectCounter = 1;

$('a').click(function() {
  function setValue() {
    projectCounter = projectCounter + 1;
    alert(window.projectCounter);
  }
});

You need to remove the var from inside the set value function. 您需要从设定值函数中删除var。 You should also move this function outside the .click event so you can use it in other locations if required. 您还应该在.click事件之外移动此功能,以便在需要时可以在其他位置使用它。

When you declare var inside a function, you are explicitly giving it a local scope. 在函数内部声明var ,您明确地给它一个局部范围。 If you leave off var when using a variable, it is always considered to be global. 如果在使用变量时不使用var ,则始终将其视为全局变量。 So, in your case, you are using var inside the function, which gives it a local scope to that function. 因此,在您的情况下,您在函数内部使用var ,这为该函数提供了局部范围。

Lastly, you aren't actually calling setValue() anywhere. 最后,你实际上并没有在任何地方调用setValue()。 So that function isn't actually executing. 所以该功能实际上并没有执行。 Did you instead mean this? 你的意思是这个吗?

var projectCounter = 1;

$('a').click(function() {
    projectCounter = projectCounter + 1;
    alert(window.projectCounter);
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM