Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
javascript [2020/09/23 16:13] 127.0.0.1 external edit |
javascript [2020/10/05 03:09] (current) paul [Object Spread Operator] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== javascript ====== | ====== javascript ====== | ||
+ | |||
+ | ===== var, let and const ==== | ||
+ | |||
+ | A '' | ||
+ | variable. It is also added as a non-configurable property to the global object. | ||
+ | |||
+ | The '' | ||
+ | accessible from blocks that are children of the block the variable was declared | ||
+ | in. | ||
+ | |||
+ | If '' | ||
+ | it. | ||
+ | |||
+ | ===== Strings ===== | ||
+ | |||
+ | Javascript **template literals** allow you to add variables in a string. | ||
+ | |||
+ | <code javascript> | ||
+ | let age = 33; | ||
+ | let name = ' | ||
+ | let out = `My name is ${name} and I am ${age}`; | ||
+ | </ | ||
+ | |||
+ | ===== Object Literals ===== | ||
+ | |||
+ | <code javascript> | ||
+ | const person = { | ||
+ | name: ' | ||
+ | }; | ||
+ | |||
+ | // You can add data willy nilly! | ||
+ | person.email = " | ||
+ | </ | ||
+ | |||
+ | ==== Object Destructuring ==== | ||
+ | |||
+ | You can pull data out of an object using a technique called object | ||
+ | destructuring. | ||
+ | |||
+ | <code javascript> | ||
+ | const person = { | ||
+ | firstName: ' | ||
+ | lastName: ' | ||
+ | age: 33, | ||
+ | address: { | ||
+ | street: '50 main st', | ||
+ | city: ' | ||
+ | state: ' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | const { firstName, lastName, address:{ city } } = person; | ||
+ | </ | ||
+ | |||
+ | ===== Arrays ===== | ||
+ | |||
+ | There are a bunch of array operations: | ||
+ | |||
+ | '' | ||
+ | '' | ||
+ | |||
+ | Iterating through arrays is a whole thing of course. There are a bunch of ways | ||
+ | to do it. You can while loop it, or you can make use of the '' | ||
+ | statement. | ||
+ | |||
+ | ==== Filter ==== | ||
+ | |||
+ | Arrays have a filter function that is very powerful. You pass in a function that | ||
+ | defines the filter criteria. Very cool. | ||
+ | |||
+ | <code javascript> | ||
+ | let applicants = [ { name: "Joe Shmoe", | ||
+ | { name: "Rob Parny", | ||
+ | { name: "Rob Parly", | ||
+ | { name: "Lem Meloo", | ||
+ | |||
+ | let onlyUsApplicants = applicants.filter(app => app.country === " | ||
+ | </ | ||
+ | |||
+ | ==== Map ==== | ||
+ | |||
+ | A '' | ||
+ | new array with returned values of the function passed into map that can act on | ||
+ | each element. | ||
+ | |||
+ | ==== Reduce ==== | ||
+ | |||
+ | A '' | ||
+ | its basic form it requires a combining function that combines to elements of the | ||
+ | array. | ||
+ | |||
+ | It can also be used to get a max or min of an array. For example: | ||
+ | |||
+ | <code javascript> | ||
+ | let data = [ { name: " | ||
+ | { name: " | ||
+ | { name: " | ||
+ | </ | ||
+ | |||
+ | ==== Some && Every ==== | ||
+ | |||
+ | '' | ||
+ | If any of the elements in the array passes the test, it returns true. | ||
+ | |||
+ | '' | ||
+ | If all of the elements in the array pas the test, it returns true. | ||
+ | |||
+ | ==== Composing ==== | ||
+ | |||
+ | '' | ||
+ | result or subset of data in a few lines of code. | ||
+ | |||
+ | <code javascript> | ||
+ | // Filter and map to a new array | ||
+ | let dirs = SCRIPTS.filter(script => script.living).map(script => { | ||
+ | return { name: script.name, | ||
+ | </ | ||
===== Functions ===== | ===== Functions ===== | ||
+ | |||
+ | There' | ||
+ | |||
+ | Using a '' | ||
+ | |||
+ | <code javascript> | ||
+ | const fun1 = function(x) { | ||
+ | return x*x; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | Using a '' | ||
+ | can call a declared function before it's declated, as they are moved to the top | ||
+ | of their scope, and all code in that scope can use them. They also don't need a | ||
+ | semicolon at the end. | ||
+ | |||
+ | <code javascript> | ||
+ | sayHello(); | ||
+ | |||
+ | function sayHello() { | ||
+ | console.log(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Using the '' | ||
+ | functions. If you don't put curly braces and have a single expression, it will | ||
+ | be returned. | ||
+ | |||
+ | <code javascript> | ||
+ | const sayHi = () => { console.log(' | ||
+ | const returnOne = () => 1; | ||
+ | </ | ||
+ | |||
+ | Functions in javascript ignore extra parameters you pass in and also add | ||
+ | undefineds for parameters you don't pass in. | ||
Functions in javascript can be named or anonymous. They have access to local variables that are in the scope of their parent functions. | Functions in javascript can be named or anonymous. They have access to local variables that are in the scope of their parent functions. | ||
Line 16: | Line 168: | ||
console.log(output) | console.log(output) | ||
} | } | ||
+ | </ | ||
+ | |||
+ | ===== Higher-Order Functions ===== | ||
+ | |||
+ | Higher-order functions are functions that operate on other functions, either by | ||
+ | passing them in as a parameter or by returning them. | ||
+ | |||
+ | The shit you can do with functions is nuts. | ||
+ | |||
+ | <code javascript> | ||
+ | let repeat = function(n, action){ | ||
+ | for(let i = 1; i <= n; i++){ | ||
+ | action(i); | ||
+ | } | ||
+ | }; | ||
+ | repeat(3, n => { console.log(n); | ||
</ | </ | ||
Line 83: | Line 251: | ||
You can embed javascript in html sources and make use of many function that can | You can embed javascript in html sources and make use of many function that can | ||
modify HTML elements. | modify HTML elements. | ||
+ | |||
+ | ===== Equality ===== | ||
+ | |||
+ | In javascript there are two equality statements, '' | ||
+ | |||
+ | Double equal: Loose equality check. Tries to coerse types so theres a match and | ||
+ | then compares content. | ||
+ | |||
+ | Triple equal: Type and content have to match. Strict equality. | ||
+ | |||
+ | ===== Control Flow ===== | ||
+ | |||
+ | ==== Switch ==== | ||
+ | |||
+ | Switch statements are wierd in that statements start executing at the | ||
+ | start of a matching label, and will keep executing accross other labels | ||
+ | until it hits a break. | ||
+ | |||
+ | In the following if you pass '' | ||
+ | |||
+ | <code javascript> | ||
+ | switch(prompt(" | ||
+ | case ' | ||
+ | console.log(" | ||
+ | break; | ||
+ | case ' | ||
+ | console.log(" | ||
+ | case ' | ||
+ | console.log(" | ||
+ | break; | ||
+ | default: | ||
+ | console.log(" | ||
+ | break; | ||
+ | } | ||
+ | </ | ||
+ | ===== OOP ===== | ||
+ | |||
+ | JavaScript OOP. | ||
+ | |||
+ | ==== Methods ==== | ||
+ | |||
+ | Methods in JS are simply properties that hold function values. | ||
+ | |||
+ | <code javascript> | ||
+ | let rabbit = {}; | ||
+ | rabbit.speak = function(line) { | ||
+ | console.log(`I am saying ' | ||
+ | } | ||
+ | |||
+ | rabbit.speak(" | ||
+ | </ | ||
+ | |||
+ | ==== Prototypes ==== | ||
+ | |||
+ | JavaScript has inheritance. Almost every object in javascript inherits (directly | ||
+ | or indirectly) from '' | ||
+ | properties, including methods, such as '' | ||
+ | |||
+ | ==== Classes ==== | ||
+ | |||
+ | You can have classes in JavaScript but they can only contain methods. They | ||
+ | cannot contain non functions, such as variables. | ||
+ | |||
+ | <code javascript> | ||
+ | class Rabbit{ | ||
+ | constructor(type){ | ||
+ | this.type = type; | ||
+ | } | ||
+ | |||
+ | speak(line) { | ||
+ | console.log(`This ${this.type} rabbit says ' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | let killaRabbit = new Rabbit(' | ||
+ | let spottedRabbit = new Rabbit(' | ||
+ | killaRabbit.speak(' | ||
+ | spottedRabbit.speak(' | ||
+ | </ | ||
+ | |||
+ | ==== call function ==== | ||
+ | |||
+ | JavaScript has a call function that can be invoked on an function and allows you | ||
+ | to explicitly pass a '' | ||
+ | |||
+ | ===== Object Spread Operator ===== | ||
+ | |||
+ | The Object Spread Operator is '' | ||
+ | |||
+ | I was really confused about it when I saw it used to spread out properties of an object that was returned by a function [[https:// | ||
+ | |||
+ | <code javascript> | ||
+ | computed: { | ||
+ | localComputed () { /* ... */ }, | ||
+ | // mix this into the outer object with the object spread operator | ||
+ | ...mapState({ | ||
+ | // ... | ||
+ | }) | ||
+ | } | ||
+ | </ | ||