Home

REACT LEARNING

Click the 'Mosh ES6' tab for code examples from when I was programming for an ES6 JavaScript tutorial by Mosh Hamedani. 'Mosh ES6' can be viewed with the developer console open to see different outputs on the various tabs. Click the 'Ziroll React' tab to explore examples for when I was programming for a React tutorial by Rob Ziroll!

COMMANDS:
CLEAR CONSOLE
varReplacements()
Mosh demonstrates the new keywords for defining variables and constants!
                //when a variable is declared with let, it is only available within that block
                //var is scoped to the function (scope is too wide)
                //const is like let, however it is not reassignable. Use let only when you need to reassign variables.

                for(let i = 0; i < 5; i++) {
                    console.log(i)
                }
                //i is not accessible here

                const x = 5;
objects()
Mosh demonstrates objects within ES6!
            const person = {
                name: "Mosh",
                //walk is a "METHOD" inside the person object
                //NEW -> we don't need to write key: function example(), etc... we can just write example()
                walk() {},
                talk() {}
            }
            person.talk()

            const targetMember = 'name'
            //We could dynamically change the property accessed in the object by assigning a variable inside the brackets
            person[targetMember] = "John"
            console.log(person.name)
thisKeyword()
Mosh demonstrates the 'this' keyword within ES6!
            const person = {
                name: "Mosh",
                walk() {
                    //'this' always returns a reference to the current object
                    console.log(this)
                },
            }
            person.walk()

            //Not calling walk method, just getting reference to the function
            const walk = person.walk
            console.log(walk)

            //walk() returns undefined... 'this' when called within an object, returns reference to the object
            //BUT in this case below, 'this' when called as a function outside of an object returns undefined --- when you are in 'strict mode'
            //If 'strict' is not enabled, it returns the window object
            walk()
binding()
Mosh demonstrates the use of the bind() function to make sure that 'this' always returns a reference to the object that your function is owned by.
            const person = {
                name: "Mosh",
                walk() {
                    console.log(this)
                },
            }
            person.walk()

            //FUNCTIONS ARE OBJECTS IN JAVASCRIPT... they have the bind method
            //When we call bind, the first argument is used to determine which object we refer to for the value of 'this'
            const walk = person.walk.bind(person)
            walk()
arrow()
Mosh demonstrates the use arrow functions (favorite functionality).
            //These two functions are exactly the same
            const square = function(number) {
                return number * number
            }
            const square2 = (number) => number*number

            console.log(square(5))
            console.log(square2(5))

            const jobs = [
                { id: 1, isActive: true},
                { id: 2, isActive: true},
                { id: 3, isActive: false}
            ]
            //Filter iterates over the array. It takes each element in the job object and returns true or false. Made easy with arrow functions.
            const activeJobs = jobs.filter(job => job.isActive)

            console.log(activeJobs)
arrowThis()
Mosh demonstrates how arrow functions work with the 'this' keyword.
            //Arrow functions do not rebind 'this'
            const person = {
                talk() {
                    //The function in the setTimeout function is not attached to the person object
                    //OLD way is to set the following variable:
                    var self = this;
                    setTimeout(function() {
                        console.log("self", self)
                    }, 1000)
                }
            }
            const person2 = {
                talk() {
                    //Arrow functions do not rebind 'this' - in other words, if we change the callback function to an arrow function
                    //It will inherit the 'this' keyword. Returns 'this' in the context where the code was called
                    setTimeout(() =>
                        console.log("this", this)
                    , 1000)
                }
            }
            person.talk()
            person2.talk()
arrayMap()
Mosh demonstrates how to use the new array method map().
            //In React, we used map() to render lists
            const colors = ['red','green','blue']
            //Pass a callback function into map() and transform each element in the array (does not modify original array)
            //We also utilize template literals using the key next to 1 (back tick) **literals not working here
            const items = colors.map(color => <li>$color}</li>)
            console.log(items)
objDest()
Mosh demonstrates how object destructuring works in JS.
            const address = {
                street: 'HI',
                city: 'MY',
                country: 'NAME'
            }
            // //These 3 lines are directly equivalent
            // const street = address.street
            // const city = address.city
            // const country = address.country

            // //To this 1 line!
            // const {street,city,country} = address

            // //Targets the street key
            // const {street} = address

            //Changes the name of the street key for use later
            const {street: st} = address

            console.log(st)
spread()
Mosh demonstrates how the spread operator works in JS.
            let first = [1,2,3]
            let second = [4,5,6]

            //Old way of doing things
            let combined = first.concat(second)

            //New way! Spread operator ... Shortens code and also lets you view the array as if it will look
            combined = [...first,'a',...second,'b']

            //Easily clone an array
            let clone = [...first]

            console.log(first)
            console.log(clone)

            //Apply spread operator on object
            first = { name: 'Mosh' }
            second = { job: 'Instructor' }
            combined = {...first,...second, location: 'Australia'}

            console.log(combined)

            clone = {...first}

            console.log(clone)
classes()
Mosh demonstrates how classes work in JS.
            //Classes are useful because duplicating objects reuses methods
            class Person {
                constructor(name) {
                    this.name = name
                }

                walk() {
                    console.log("walk")
                }
            }
            const person = new Person("Paul")