Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="description" content="functions">
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /* 1. Functions:
- * Functions are programs within programs or, a type of data encapsulating reusable
- * code. To start with, we must declare or define a function. A function takes
- * a parameter and runs it through a block of code. But how does it do that?
- * This leads to the second part of using functions. Once we have declared our
- * function, we need to invoke it. When we invoke or call our function, we pass an
- * argument through our parameter slot. Let's look at a function.
- */
- function nameHorse(name) { //Declares the function, nameHorse, and defines our parameter.
- console.log("My horse's name is " + name); //This is what the function will do.
- }
- nameHorse("pickle"); //Our invocation where we pass in our argument.
- nameHorse("taco"); /*This prints taco. The above prints pickle, and the only thing that
- * changed is our argument.
- */
- /* The parameter is simply a placeholder for input, whereas the argument is the
- * actual input. Our parameter, name, takes in the argument or input, of whatever we want.
- */
- // Named functions are functions that have been assigned to a variable or constant.
- multiply(9, 14); //This has been added to demonstrate hoisting to scope. Prints 126.
- function multiply(number1, number2) {
- console.log(number1 * number2);
- }
- multiply(12, 44);
- /* This invokes our function and logs 528, the result of our arguments
- * passing through our function. It's important to note that named functions are hoisted
- * to the top of their scope. This means that I can invoke this function earlier in
- * our program. Now let's try assigning a function to a variable.
- */
- var tuxedo = function(color){return "Slick " + color;};
- var purchase = tuxedo("blue");
- console.log(purchase); // This logs "Slick blue".
- /* In the above, we created a variable and assigned a function to it. We then invoked
- * the function and assigned the invocation to yet another variable. We also gave an
- * input, and returned a value, which functions can do optionally.
- */
- //Let's look at copy by value vs. copy by reference.
- var gravity = 45;
- var flying = gravity;
- console.log(flying); //logs 45.
- flying = 99;
- console.log(gravity);//logs 45.
- console.log(flying); //logs 99.
- // Here we have copy by value, which links to the value in the container.
- var baseBall = {bat: "wood"};
- var game = baseBall;
- console.log(game.bat);//logs "wood".
- game.bat = "aluminium";
- console.log(baseBall.bat); //"logs "aluminium".
- /*Here we have copy by reference where the containers are both linked to the same object
- * and editing the object from one container, changes the values for both.
- */
- /*Scopes! Scopes, local and global, determine where in a program a variable or value
- * can be used. Local is enclosed with a function, and cannot be accessed from outside of
- * that function. Global can be accessed anywhere in the program after it is declared.
- */
- function talkDirtyToMe() {
- var nasty = 3;
- console.log(nasty);
- }
- talkDirtyToMe();//prints 3
- //console.log(nasty); //will not print, returns error. commented out.
- console.log(chicken);//returns undefined.
- var chicken = "cluck";
- console.log(chicken);//returns "cluck".
- /* Finally, closures are a fancy pants way of saying a function inside a function
- * can access values in the parent scope.
- */
- function cooker(food) {
- var topping = "cheese";
- return function(string) {
- return (string + topping + food);
- }
- }
- console.log(cooker("Hello"), ("duck"));//Prints the return and "duck".
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/* 1. Functions:
- * Functions are programs within programs or, a type of data encapsulating reusable
- * code. To start with, we must declare or define a function. A function takes
- * a parameter and runs it through a block of code. But how does it do that?
- * This leads to the second part of using functions. Once we have declared our
- * function, we need to invoke it. When we invoke or call our function, we pass an
- * argument through our parameter slot. Let's look at a function.
- */
- function nameHorse(name) { //Declares the function, nameHorse, and defines our parameter.
- console.log("My horse's name is " + name); //This is what the function will do.
- }
- nameHorse("pickle"); //Our invocation where we pass in our argument.
- nameHorse("taco"); /*This prints taco. The above prints pickle, and the only thing that
- * changed is our argument.
- */
- /* The parameter is simply a placeholder for input, whereas the argument is the
- * actual input. Our parameter, name, takes in the argument or input, of whatever we want.
- */
- // Named functions are functions that have been assigned to a variable or constant.
- multiply(9, 14); //This has been added to demonstrate hoisting to scope. Prints 126.
- function multiply(number1, number2) {
- console.log(number1 * number2);
- }
- multiply(12, 44);
- /* This invokes our function and logs 528, the result of our arguments
- * passing through our function. It's important to note that named functions are hoisted
- * to the top of their scope. This means that I can invoke this function earlier in
- * our program. Now let's try assigning a function to a variable.
- */
- var tuxedo = function(color){return "Slick " + color;};
- var purchase = tuxedo("blue");
- console.log(purchase); // This logs "Slick blue".
- /* In the above, we created a variable and assigned a function to it. We then invoked
- * the function and assigned the invocation to yet another variable. We also gave an
- * input, and returned a value, which functions can do optionally.
- */
- //Let's look at copy by value vs. copy by reference.
- var gravity = 45;
- var flying = gravity;
- console.log(flying); //logs 45.
- flying = 99;
- console.log(gravity);//logs 45.
- console.log(flying); //logs 99.
- // Here we have copy by value, which links to the value in the container.
- var baseBall = {bat: "wood"};
- var game = baseBall;
- console.log(game.bat);//logs "wood".
- game.bat = "aluminium";
- console.log(baseBall.bat); //"logs "aluminium".
- /*Here we have copy by reference where the containers are both linked to the same object
- * and editing the object from one container, changes the values for both.
- */
- /*Scopes! Scopes, local and global, determine where in a program a variable or value
- * can be used. Local is enclosed with a function, and cannot be accessed from outside of
- * that function. Global can be accessed anywhere in the program after it is declared.
- */
- function talkDirtyToMe() {
- var nasty = 3;
- console.log(nasty);
- }
- talkDirtyToMe();//prints 3
- //console.log(nasty); //will not print, returns error. commented out.
- console.log(chicken);//returns undefined.
- var chicken = "cluck";
- console.log(chicken);//returns "cluck".
- /* Finally, closures are a fancy pants way of saying a function inside a function
- * can access values in the parent scope.
- */
- function cooker(food) {
- var topping = "cheese";
- return function(string) {
- return (string + topping + food);
- }
- }
- console.log(cooker("Hello"), ("duck"));//Prints the return and "duck".
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement