virtualw3schools Posted March 22, 2012 Share Posted March 22, 2012 Hello to all, I have read this article http://www.w3schools...s_functions.aspbut what I don't understand is, why does he call variable to what its wrapped with parentheses, if it has no "var" before? Link to comment Share on other sites More sharing options...
boen_robot Posted March 22, 2012 Share Posted March 22, 2012 Normally, the "var" keyword is required to make it explicit that you want a new variable, rather than reusing one from a higher/global scope.The line function product(A, is a function declaration.A function declaration is not allowed to reuse variables, since the value of those reused variables would only be known when you call the function later on, thus rendering the function uncompilable (=> REALLY slow). Because of this, the "var" keyword is not required for specifying parameters, and is in fact completely eliminated from the syntax at that point, to make JavaScript simpler. Link to comment Share on other sites More sharing options...
virtualw3schools Posted March 22, 2012 Author Share Posted March 22, 2012 Thanks, but I still don't understand. Sorry, I just don't get the parameters thing Link to comment Share on other sites More sharing options...
boen_robot Posted March 22, 2012 Share Posted March 22, 2012 A parameter is just like a variable, except that its value is determined when you call the function.For example, function product(A,{return A*B;} defines a function called "product" that takes two parameters. With the part (A, we're saying that within the function, the variable "A" will refer to the first parameter, and "B", to the second one.If later in the code you have product(2,3) that would be you calling the function with two values - one for each parameter. The first one being 2, and the second one being 3.Since the function internally refers to the first parameter as "A", and the second "B", the part return A*B; becomes equivalent to return 2*3; which, of course, results in 6. Link to comment Share on other sites More sharing options...
virtualw3schools Posted March 23, 2012 Author Share Posted March 23, 2012 A parameter is just like a variable, except that its value is determined when you call the function. For example,function product(A,{return A*B;} defines a function called "product" that takes two parameters. With the part (A, we're saying that within the function, the variable "A" will refer to the first parameter, and "B", to the second one. If later in the code you have product(2,3) that would be you calling the function with two values - one for each parameter. The first one being 2, and the second one being 3. Since the function internally refers to the first parameter as "A", and the second "B", the part return A*B; becomes equivalent to return 2*3; which, of course, results in 6. Oh thank you , that explanation was really helpful! Now I can continue Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.