Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. /*
  2. javascript methods
  3.  
  4. types:
  5.  
  6. 1 method with return and with parameter
  7. 2 method without return and wothout parameter
  8. 3 method with return and wothout parameter
  9. 4 method without return and with parameter
  10. */
  11.  
  12. // type 1
  13. function add(a,b) {
  14. return a+b;
  15. }
  16.  
  17. // calling the function by holding
  18. h = add(1,2);
  19. console.log(h);
  20.  
  21. // calling the function direct to print in console
  22. console.log(add(1,2));
  23.  
  24. // type 2
  25. function pi(){
  26. console.log(3.14);
  27. }
  28.  
  29. // calling function
  30. pi();
  31.  
  32. console.log("--------scope--------");
  33. // scope of variable
  34.  
  35. var a = 10;
  36.  
  37. function safe(){
  38. var a = 20;
  39. var b = 10;
  40. c = 30;
  41. console.log(a);
  42. console.log(b);
  43. console.log(c);
  44. }
  45. safe();
  46. console.log(a);
  47. // console.log(b) // this is not accessable outside of the function
  48. console.log(c);
  49.  
  50. console.log("--------block statement--------");
  51. {
  52. var a = 13;
  53.  
  54. }
  55. console.log(a);
  56.  
  57. {
  58. let e = 12;
  59. console.log(e);
  60. }
  61.  
  62. // console.log(e) // this is not accessable outside the block
  63. console.log("--------constant--------");
  64. const pit = 3.14;
  65. var ti = 21;
  66. console.log(ti,pit);
  67.  
  68. var ti = 22;
  69. // var pit = 22/7;
  70. console.log(ti,pit)
  71.  
  72. {
  73. let gb = 12;
  74. console.log(gb);
  75. }
  76. {
  77. var gb = 13;
  78. console.log(gb);
  79. // var pit = 22/7;
  80. }
  81. console.log("--------class and object--------");
  82.  
  83. /*
  84. SYNTAX :
  85. class classname{
  86. functionname(para){
  87. return;
  88. }
  89. }
  90.  
  91. object = new classname();
  92. */
  93.  
  94. class sample{
  95. name = "jack" // it is a class variable not a global variable
  96. print() {
  97. return this.name
  98. }
  99. }
  100.  
  101. s = new sample();
  102. var a = s.print();
  103. console.log(a);
  104. console.log(s.name)
  105.  
  106. console.log("--------polymorphism and overloading--------");
  107. class animal{
  108. dog(breed){
  109. console.log("i am one",breed);
  110. }
  111. dog(breed,paw){
  112. console.log("i am two",breed,paw);
  113. }
  114. }
  115. a = new animal();
  116. console.log(a.dog("dober"));
  117. console.log(a.dog("dober",2));
  118.  
  119. class animal_one{
  120. dog(breed){
  121. console.log("i am one",breed);
  122. }
  123. }
  124. class animal_two{
  125. dog(breed,paw){
  126. console.log("i am two",breed,paw);
  127. }
  128. }
  129.  
  130. b = new animal_one();
  131. console.log(b.dog("dober"));
  132.  
  133. c = new animal_two();
  134. console.log(c.dog("dober",2));
  135.  
  136. console.log("--------constructor--------");
  137.  
  138. class one{
  139. constructor(a,b,c){
  140. this.a = a;
  141. this.b = b;
  142. this.c = c;
  143. }
  144. basic(){
  145. console.log(this.a,this.b,this.c);
  146. }
  147. }
  148.  
  149. d = new one(1,2,3);
  150. d.basic();
  151.  
  152.  
  153. class two{
  154. constructor(){
  155. this.a = 1;
  156. this.b = 2;
  157. this.c = 3;
  158. }
  159. basic_2(){
  160. console.log(this.a,this.b,this.c);
  161. }
  162. }
  163.  
  164. e = new two();
  165. e.basic_2();
  166.  
  167. console.log("--------inheritance and overrideing--------");
  168.  
  169. class anml{
  170. dog(){
  171. console.log("wof");
  172. }
  173. cat(){
  174. console.log("mew");
  175. }
  176. }
  177. class pet extends anml{
  178. dog(){
  179. console.log("bow");
  180. }
  181. }
  182.  
  183. o = new anml();
  184. o.dog();
  185. o.cat();
  186.  
  187. p = new pet();
  188. p.dog(); // over rided
  189. p.cat();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement