标签:
A function takes in inputs, does something with them, and produces an output.
1 // This is what a function looks like: 2 3 var divideByThree = function (number) { 4 var val = number / 3; 5 console.log(val); 6 }; 7 8 // On line 12, we call the function by name 9 // Here, it is called ‘dividebythree‘ 10 // We tell the computer what the number input is (i.e. 6) 11 // The computer then runs the code inside the function! 12 divideByThree(10);
var
, and then give it a name dividByThree. The name should begin with a lowercase letter and the convention is to use lowerCamelCase where each word (except the first) begins with a capital letter.function
keyword to tell the computer that you are making a function{ }
. Every line of code in this block must end with a ;
.
function square(number) { return number * number; }
{ };
Such a function can be anonymous; it does not have to have a name.
var square = function(number) { return number * number }; var x = square(4) // x gets the value 16
Variables defined outside a function are accessible anywhere once they have been declared. They are calledglobal variables and their scope is global.
Variables defined inside a function are local variables. They cannot be accessed outside of that function.
标签:
原文地址:http://www.cnblogs.com/elewei/p/5641767.html