Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Animal from './Animal';
  2. const limit = 200;
  3.  
  4. {
  5.     let limit = 10;
  6.     console.log('backstage limit', limit);
  7. }
  8. console.log('Overall limit', limit);
  9.  
  10. let a = `good`;
  11. let greeting = `${a} mornin`;
  12. console.log(greeting);
  13.  
  14. // The Spread Operator {...}
  15. // Rest Parameters funtcion(...) {};
  16. // Destructuring Assignment simplifies extracting data on arrays and objects into distinct variables.
  17.  
  18. let c = [20, 30, 40];
  19. let d = [10, ...c, 50];
  20. console.log(d);
  21.  
  22. let e = ['Brian','Olga','Matteo','Roy'];
  23. let f = ['Jason','Harit','Sandie','Richard', ...e];
  24. console.log(f)
  25.  
  26. // Okay, let`s do a little function where we have a spread operator as a parameter :)
  27. function collect(...z){
  28.     console.log(z);
  29. }
  30.  
  31. collect(1,2,3,4,5,6,7,8,9,5,44);
  32.  
  33. // Next lesson, Destructuring assignment
  34.  
  35. //let y = [4,5,6];
  36. //let four = y(0);
  37. //let five = y(1);
  38.  
  39. // that above takes to long right, let`s do this.
  40.  
  41. let n = [4,5,6];
  42. let [four,five] = n;
  43. console.log(four,five);
  44.  
  45. // Helper Methods
  46. // The map method creates new arrays by calling a function on individual elements in an original array
  47. // The filter method creates new arrays based on an original array and a certain test on each of its element.
  48.  
  49. // Modules
  50. //Split code into unique files based on relevant data.
  51. // Handled in es6 via the export and import keywords.
  52.  
  53.  
  54. // lets explore the arrow functions.
  55. // first the old way below, it`s gay.
  56. function cheer() {
  57.     console.log("Whoohoo!");
  58. }
  59.  
  60. cheer();
  61.  
  62. // watch this
  63. var cheer2 = function() {
  64.     console.log("Fuuk Yeah");
  65. }
  66.  
  67. cheer2();
  68.  
  69. // timeout function
  70. setTimeout(function() {
  71.     console.log("delayed whoohoo?!");
  72. }, 3000);
  73.  
  74. // New way with arrow function, hence the name see the change
  75.  
  76. setTimeout(() => {
  77.     console.log("delayed even more whoohoo?!");
  78. }, 3500);
  79.  
  80.  
  81. // easily iterating through arrays, and adding a function to it without writing loops. Thank you Map Helper!
  82. let values = [20,30,40,50,60,70];
  83.  
  84.  
  85. let doubled = values.map((n) => n*2);
  86. console.log(doubled);
  87.  
  88. /*
  89. let points = [11,22,55,234,1,452,6,43];
  90. let highScores = points.filter((n) => {
  91.     return n > 22;
  92. })
  93. console.log(highScores);
  94. */
  95.  
  96. // new method
  97. let points = [11,22,55,234,1,452,6,43];
  98. let highScores = points.filter((n) => n > 15);
  99. console.log(highScores);
  100.  
  101.  
  102. // String Helper methdos! more cool stuff
  103. console.log("butterfly".startsWith("butter"));
  104. console.log("butterfly".endsWith("fly"));
  105. console.log("butterfly".includes("fly"));
  106. console.log("butterfly".includes("tt"));
  107. console.log("butterfly".includes("but"));
  108. console.log("butterfly".includes("cunt"));
  109.  
  110.  
  111. // number check
  112. const addToCart = (item, number) => {
  113.     return Number.isSafeInteger(number);
  114. }
  115.  
  116. console.log(addToCart('Shirt', Math.pow(2, 54)));
  117.  
  118. import {fellowship, total} from './fellowship';
  119. import {add, multiply} from './math';
  120. console.log(fellowship, total);
  121. console.log(multiply(5,10));
  122. console.log(add(5,10));
  123.  
  124. //Classes
  125.  
  126. class Lion extends Animal {
  127.     constructor(name, height, color){
  128.         super(name, height);
  129.         this.color = color;
  130.     }
  131.     hello() {
  132.         console.log(`Hi I'm ${this.name} from the Pride Rock`);
  133.    }
  134. }
  135.  
  136. let son = new Lion("Simba", 2, "Golden");
  137.  
  138. let king  = new Animal('Lion', 4.5);
  139. king.hello();
  140. console.log(son);
  141. son.hello();
  142.  
  143.  
  144.  
  145. //Prototypes
  146.  
  147. function Wizard(name,house,pet) {
  148.    this.name = name;
  149.    this.house = house;
  150.    this.pet = pet;
  151.    
  152.    this.greet = () => {
  153.        return `i'm ${this.name} from ${this.house}`
  154.     }
  155. }
  156.  
  157. Wizard.prototype.pet_name;
  158.  
  159. Wizard.prototype.info = function() {
  160.     return `I have a ${this.pet} named ${this.pet_name}`
  161. }
  162. let harry = new Wizard("Harry Potter", "Gryffindor", "Owl");
  163. harry.pet_name = "Hedwig";
  164. console.log(harry.info());
  165.  
  166. //Datastructures, Sets, Maps
  167.  
  168. // The Set - set stores unique values. No duplicates allowed.
  169. let numbers = [3, 5, 3,2, 6, 44, 4453, 3, 23, 535]
  170. let chars = "sdkjfhlkdasfjhakdsjfhkjjhgasdkjfhgdskjforhmsuhqweiuyfwqeofsdncvioscqpdsmdsbsj4";
  171. let chars_arr = chars.split("");
  172. let chars_set = new Set(chars_arr);
  173. let numSet = new Set(numbers);
  174.  
  175. // Enhanced For Loop
  176. for(let element of numSet.values()) {
  177.     console.log(element);
  178. }
  179.  
  180. console.log(chars_set);
  181.  
  182. //Maps have keys and values
  183. let mapper = new Map();
  184. let key_1 = "string key";
  185. let key_2 = { a: 'key'}
  186. let key_3 = function() {};
  187. mapper.set(key_1, 'return value for string key');
  188. mapper.set(key_2, 'return value for an object key');
  189. mapper.set(key_3, 'return value for a function key');
  190. console.log(mapper);
  191.  
  192.  
  193. // more maps
  194. let numArr = [[1, 'one'], [2, 'two'], [3, 'three']];
  195. let valMap = new Map(numArr);
  196.  
  197. for (let [key, value] of valMap.entries()){
  198.     console.log(`${key} point to ${value}`);
  199. }
  200.  
  201.  
  202.  
  203. let chars3 = "sdkjfhlkdasfjhakdsjfhkjjhgasdkjfhgdskjforhmsuhqweiuyfwqeofsdncvioscqpdsmdsbsj4";
  204. let letters = new Map();
  205. for(let i=0; i<chars3.length; i++){
  206.     let letter = chars3[i];
  207.     if(!letters.has(letter)){
  208.         letters.set(letter,1)
  209.     } else {
  210.         letters.set(letter, letters.get(letter) + 1);
  211.     }
  212. }
  213.  
  214. console.log(letters);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement