This is probably a stupidly basic question for this community, but if someone could explain it to me I would be very great full, I am so confused by it. I found this tutorial on the net and this was example.
<script type="text/javascript">
开发者_高级运维 function sports (x){
alert("I love " + x);
}
sports ("Football");
sports ("Rally");
sports ("Rugby");
</script>
Why does this display the 3 variables: Football, Rally and Rugby?
Is it because x = sports? So when the variables of sports are defined they get displayed?
I think I confused myself more when writing this so I hope it kind of makes sense :(
Unless I'm missing something, the reason it shows all three variables (sequentially, not simultaneously) is because you're calling the function three times, each time you call it you're passing a variable ("Football"
for example), which the function uses internally to complete the alerted message.
You're defining a function, called "sports", that takes one argument, named "x". Each time you call the function, it alerts a message, substituting the argument you pass in for "x". In this example, you call the function three times, with three different values of "x".
I hope this helps you, x is a container for a value. So when you say something like sports ("Football");
it behaves as if this:
alert("I love " + x);
was actually this:
alert("I love " + "Football");
This is because x
contains "Football"
.
Think of it as a placeholder for a value of some kind.
Every time you write sports("text") you call a function. It means that it is executed.
Your function is displaying an alert message using an argument. In your case you execute your function three times with 3 different arguments.
精彩评论