Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Animal from './Animal';
- const limit = 200;
- {
- let limit = 10;
- console.log('backstage limit', limit);
- }
- console.log('Overall limit', limit);
- let a = `good`;
- let greeting = `${a} mornin`;
- console.log(greeting);
- // The Spread Operator {...}
- // Rest Parameters funtcion(...) {};
- // Destructuring Assignment simplifies extracting data on arrays and objects into distinct variables.
- let c = [20, 30, 40];
- let d = [10, ...c, 50];
- console.log(d);
- let e = ['Brian','Olga','Matteo','Roy'];
- let f = ['Jason','Harit','Sandie','Richard', ...e];
- console.log(f)
- // Okay, let`s do a little function where we have a spread operator as a parameter :)
- function collect(...z){
- console.log(z);
- }
- collect(1,2,3,4,5,6,7,8,9,5,44);
- // Next lesson, Destructuring assignment
- //let y = [4,5,6];
- //let four = y(0);
- //let five = y(1);
- // that above takes to long right, let`s do this.
- let n = [4,5,6];
- let [four,five] = n;
- console.log(four,five);
- // Helper Methods
- // The map method creates new arrays by calling a function on individual elements in an original array
- // The filter method creates new arrays based on an original array and a certain test on each of its element.
- // Modules
- //Split code into unique files based on relevant data.
- // Handled in es6 via the export and import keywords.
- // lets explore the arrow functions.
- // first the old way below, it`s gay.
- function cheer() {
- console.log("Whoohoo!");
- }
- cheer();
- // watch this
- var cheer2 = function() {
- console.log("Fuuk Yeah");
- }
- cheer2();
- // timeout function
- setTimeout(function() {
- console.log("delayed whoohoo?!");
- }, 3000);
- // New way with arrow function, hence the name see the change
- setTimeout(() => {
- console.log("delayed even more whoohoo?!");
- }, 3500);
- // easily iterating through arrays, and adding a function to it without writing loops. Thank you Map Helper!
- let values = [20,30,40,50,60,70];
- let doubled = values.map((n) => n*2);
- console.log(doubled);
- /*
- let points = [11,22,55,234,1,452,6,43];
- let highScores = points.filter((n) => {
- return n > 22;
- })
- console.log(highScores);
- */
- // new method
- let points = [11,22,55,234,1,452,6,43];
- let highScores = points.filter((n) => n > 15);
- console.log(highScores);
- // String Helper methdos! more cool stuff
- console.log("butterfly".startsWith("butter"));
- console.log("butterfly".endsWith("fly"));
- console.log("butterfly".includes("fly"));
- console.log("butterfly".includes("tt"));
- console.log("butterfly".includes("but"));
- console.log("butterfly".includes("cunt"));
- // number check
- const addToCart = (item, number) => {
- return Number.isSafeInteger(number);
- }
- console.log(addToCart('Shirt', Math.pow(2, 54)));
- import {fellowship, total} from './fellowship';
- import {add, multiply} from './math';
- console.log(fellowship, total);
- console.log(multiply(5,10));
- console.log(add(5,10));
- //Classes
- class Lion extends Animal {
- constructor(name, height, color){
- super(name, height);
- this.color = color;
- }
- hello() {
- console.log(`Hi I'm ${this.name} from the Pride Rock`);
- }
- }
- let son = new Lion("Simba", 2, "Golden");
- let king = new Animal('Lion', 4.5);
- king.hello();
- console.log(son);
- son.hello();
- //Prototypes
- function Wizard(name,house,pet) {
- this.name = name;
- this.house = house;
- this.pet = pet;
- this.greet = () => {
- return `i'm ${this.name} from ${this.house}`
- }
- }
- Wizard.prototype.pet_name;
- Wizard.prototype.info = function() {
- return `I have a ${this.pet} named ${this.pet_name}`
- }
- let harry = new Wizard("Harry Potter", "Gryffindor", "Owl");
- harry.pet_name = "Hedwig";
- console.log(harry.info());
- //Datastructures, Sets, Maps
- // The Set - set stores unique values. No duplicates allowed.
- let numbers = [3, 5, 3,2, 6, 44, 4453, 3, 23, 535]
- let chars = "sdkjfhlkdasfjhakdsjfhkjjhgasdkjfhgdskjforhmsuhqweiuyfwqeofsdncvioscqpdsmdsbsj4";
- let chars_arr = chars.split("");
- let chars_set = new Set(chars_arr);
- let numSet = new Set(numbers);
- // Enhanced For Loop
- for(let element of numSet.values()) {
- console.log(element);
- }
- console.log(chars_set);
- //Maps have keys and values
- let mapper = new Map();
- let key_1 = "string key";
- let key_2 = { a: 'key'}
- let key_3 = function() {};
- mapper.set(key_1, 'return value for string key');
- mapper.set(key_2, 'return value for an object key');
- mapper.set(key_3, 'return value for a function key');
- console.log(mapper);
- // more maps
- let numArr = [[1, 'one'], [2, 'two'], [3, 'three']];
- let valMap = new Map(numArr);
- for (let [key, value] of valMap.entries()){
- console.log(`${key} point to ${value}`);
- }
- let chars3 = "sdkjfhlkdasfjhakdsjfhkjjhgasdkjfhgdskjforhmsuhqweiuyfwqeofsdncvioscqpdsmdsbsj4";
- let letters = new Map();
- for(let i=0; i<chars3.length; i++){
- let letter = chars3[i];
- if(!letters.has(letter)){
- letters.set(letter,1)
- } else {
- letters.set(letter, letters.get(letter) + 1);
- }
- }
- console.log(letters);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement