简体   繁体   English

为什么使用函数参数。 全局变量?

[英]Why Use Function Parameters Vs. Global Variables?

Yes, I know that this is probably a very stupid question, but this has been bugging me for a while. 是的,我知道这可能是一个非常愚蠢的问题,但这已经困扰了我一段时间。

Ok, so I have been learning JavaScript for a while now and have understood everything perfectly. 好吧,所以我已经学习了一段时间的JavaScript并完全理解了一切。 . .except for function "parameters" (I believe they are called). .except函数“参数”(我相信它们被称为)。

I was taught that they work like so: 我被教导他们这样工作:

 function test(number) { document.write(number); }; test(1); test(12); 

This makes perfect sense to me. 这对我来说非常有意义。 However, recently, I've come across some that were a little different. 然而,最近,我遇到了一些有点不同的东西。

 var counterDays = document.getElementById('days'); var counterHours = document.getElementById('hours'); var counterMinutes = document.getElementById('minutes'); var counterSeconds = document.getElementById('seconds'); var date = new Date('December 28, 2016 00:00:00'); function updateTimer(date) { var time = date - new Date(); return { 'days': Math.floor(time / (1000 * 60 * 60 * 24)), 'hours': Math.floor((time/(1000 * 60 * 60)) % 24), 'minutes': Math.floor((time / 1000 / 60) % 60), 'seconds': Math.floor((time / 1000) % 60), 'total': time }; }; function startTimer(counterDays, counterHours, counterMinutes, counterSeconds, date) { var timerInterval = setInterval(function() { var timer = updateTimer(date); //Changes the text of the 'counter' counterDays.innerHTML = timer.days; counterHours.innerHTML = timer.hours; counterMinutes.innerHTML = timer.minutes; counterSeconds.innerHTML = timer.seconds; window.onload = function() { startTimer(counterDays, counterHours, counterMinutes, counterSeconds, date); }; 
  <span id="days">&nbsp;</span> <span id="hours">&nbsp;</span> <span id="minutes">&nbsp;</span> <span id="seconds">&nbsp;</span> 

What I seriously do not understand is why the updateTimer always needs date within the parentheses, when the variable date is an already existing variable within the global scope. 我严重不明白的是,为什么updateTimer总是需要date的括号内,当变量date是在全球范围内已经存在的变量。 Same with startTimer . startTimer相同。 I don't understand why I need to pass that in. Why not just access the variable within the function, as they do have a global scope, and be done with it. 我不明白为什么我需要传递它。为什么不只是访问函数中的变量,因为它们确实具有全局范围,并且完成它。 Instead I need to pass in the variable as a parameter, for the function to work. 相反,我需要将变量作为参数传入,以使函数工作。

I've tried and tried, but the only way to make the function work is by passing in the variables. 我已经尝试过,但是使函数工作的唯一方法是传入变量。 Why is that??? 这是为什么???

As I am still learning, I've searched the internet for more information on functions and their parameters, but all show me something similar to my first example. 在我还在学习的时候,我在互联网上搜索了有关函数及其参数的更多信息,但所有这些都向我展示了与我的第一个例子类似的东西。 I know this is all probably just going over my head, but for the life of me, I just do not understand. 我知道这可能只是我的头脑,但对于我的生活,我只是不明白。

Note: I am still learning, so sorry if this whole question is plain stupid. 注意:我还在学习,很抱歉,如果整个问题都是愚蠢的。

Also, the the code for the JS that I am having a problem with won't actually run. 此外,我遇到问题的JS代码实际上不会运行。 This is due to me not wanting to put in all of my code, but rather just the code I am having trouble with. 这是因为我不想放入我的所有代码,而只是我遇到的代码。

Instead I need to pass in the variable as a parameter, for the function to work. 相反,我需要将变量作为参数传入,以使函数工作。

You dont need to define your functions with parameters. 您不需要使用参数定义函数。 You can invoke them leveraging higher scope variables 您可以利用更高范围的变量来调用它们

https://developer.mozilla.org/en-US/docs/Glossary/Scope https://developer.mozilla.org/en-US/docs/Glossary/Scope

This: 这个:

var x = 'baz';
function foo(x) {
  return x;
}
foo(x);

will do the same thing as: 会做同样的事情:

var x = 'baz';
function foo() {
  return x;
}
foo();

Writing functions that take parameters as input helps keep your code modular and side effect free among many other benefits... 编写以参数作为输入的函数有助于保持代码模块化和副作用免于许多其他好处......

1.) The second example will always throw an error if x is not accessible at a higher scope 1.)如果在较高范围内无法访问x,则第二个示例将始终抛出错误

2.) If another function mutated the value of x it would affect the output of the second example and would lead to unexpected and potentially hard to debug behavior in your application. 2.)如果另一个函数改变了x的值,它将影响第二个示例的输出,并且会导致应用程序中出现意外且可能难以调试的行为。 Whereas I can always be sure of the output of the first example 而我总能确定第一个例子的输出

3.) It is much easier to read through and maintain code that is written in the style of the first example 3.)读取和维护以第一个示例的样式编写的代码要容易得多

As I see see your code 我看到你的代码

var timer = updateTimer(date);

Kindly remove date parameter here as well as in the called function. 请在此处以及被调用函数中删除日期参数。 Now the date variable will work as in global scope. 现在,日期变量将在全局范围内起作用。 So it will be 所以它会

function updateTimer() 
{
//date variable will be present here as global variable    
 }
 var timer = updateTimer();

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

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