Advertisement
Guest User

Untitled

a guest
May 4th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1.  
  2. #JavaScript Functions
  3.  
  4. ##Objectives
  5.  
  6. - Understand what a JavaScript function is
  7. - Be able to recognize a function syntax
  8. - Understand how to write a constructor function
  9.  
  10. ###What is a JavaScript function?
  11.  
  12. `In JavaScript, a function can be both a method AND a class.`
  13.  
  14. ###Using JS functions as a subroutine
  15.  
  16. A JavaScript function is essentially a block of code to do a particular task. Sometimes you will hear them being referred to as methods or subroutines. In the Object-Oriented Programming (OOP) paradigm, think of it like behaviors.
  17.  
  18. - To use JavaScript as a method, you will have to 1) define the method and 2) invoke it.
  19.  
  20. **`Demo`**
  21.  
  22. // Define our function with a parameter name
  23.  
  24. function sayHi(name){
  25. // write a message saying hi
  26. console.log('Hi, I am ' + name);
  27. }
  28.  
  29. // Invoke our function with the argument 'Stanley'
  30.  
  31. sayHi('Stanley');
  32.  
  33. ####Activity
  34. ***
  35.  
  36. Write a function that does a square root of any integer argument and test it on the numbers 4, 6, and 22
  37.  
  38. #####Part II
  39.  
  40. Write a check in the function that ensures only a number can be inserted. Return false if the parameter is not an integer
  41.  
  42. **Solution:**
  43.  
  44. function squareRoot(num){
  45. if(typeof num==='number' && (num%1)===0){
  46. return num * num;
  47. }else{
  48. return false;
  49. }
  50. }
  51.  
  52. console.log(square(4));
  53. console.log(square(6));
  54. console.log(square(8));
  55. console.log(square('hello world'));
  56.  
  57. #####Part III
  58.  
  59. Partner up with the person next to you and create a JS function that convert Fahrenheit to Celsius
  60.  
  61. function toCelsius(fahrenheit){
  62. return (fahrenheit - 32)/(1.8);
  63. }
  64.  
  65. console.log(toCelsius(100));
  66.  
  67. ###What the heck is a class?
  68.  
  69. A class is a simply template to create objects!
  70.  
  71. For instance, let's create a human class
  72.  
  73. // Define the class
  74. function Human(name){
  75.  
  76. this.name = name;
  77. this.sayHi = function(){
  78. console.log('Hi, my name is ' + this.name);
  79. }
  80.  
  81. // Initialize the object utilizing the 'new' keyword
  82. var Stanley = new Human('Stanley');
  83. Stanley.sayHi();
  84.  
  85. ###What the heck is `this`
  86.  
  87. The `this` keyword in javascript, in a constructor function context, refers to the the Object once its been initialized with the `new` keyword.
  88.  
  89. However, keep in mind, in a global context, `this` refers to the window object
  90.  
  91. ####Activity
  92. ***
  93.  
  94. Now, make your person be able to calculate the square root of numbers
  95.  
  96. Then
  97.  
  98. Make your person be able to convert fahrenheit to celsius!
  99.  
  100. ###Questions
  101.  
  102. - What are the two main purposes of using functions in JavaScript?
  103. - What is this keyword refer to in the global context?
  104. - What is the syntax for writing a basic subroutine?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement