23
React Intro
Summary
node
in the Terminal.Tab
key to get a list of Node commands.node
command. For instance, node name-of-script.js
.console.log()
happens to use a method called process.stdout.write()
under the hood. stdout
is short for standard output. Standard input is for data streaming into a program while standard output is for data streaming out of a program.require
statement to add a script's functionality to the Node REPL. For instance: require('./code-to-be-required.js')
. A relative path must be included.Terminology
Examples
We can do this if we are writing object-oriented code:
let x = 1
x = 2 + 1
In functional code, we want to always use
const
like this:const x = 1
const newX = 2 + x
Terminology
Terminology
- Always returns an output
- Has no side effects
- Does not rely on external variables or state
- Always returns the same answer for a given input
Why pure functions?
Terminology
Terminology
Here's an example. The anonymous function that takes
yourName
as a parameter has access to the salutation
from the outer welcome function
:function welcome(salutation) {
return function(yourName) {
return `${salutation}! Nice to meet you, ${yourName}!`
}
}
Terminology
Here's an uncurried function that takes three arguments:
function aThingIMaybeLike(howMuchILikeIt, thing, reason) {
return `I ${howMuchILikeIt} ${thing} because ${reason}.`;
}
Here's how the function looks after it's been curried:
function aThingIMaybeLike(howMuchILikeIt) {
return function(thing) {
return function(reason) {
return `I ${howMuchILikeIt} ${thing} because ${reason}.`;
}
}
}
Terminology
Here's an example of a recursive function:
const incrementCounter = (counter) => {
if (counter >= 3) {
return counter;
} else {
console.log(counter);
return incrementCounter(counter + 1);
}
}
incrementCounter(0);
Terminology
Terminology
...
that is used to do the following:
- Make shallow copies of objects
- Merge multiple objects together
- Combine arrays
- Pass multiple arguments into a function
Here is the spread operator is making a shallow copy:
const myCat = {
name: "Murphy",
age: 1
}
const anotherCat = {...myCat};
Here it is merging three objects together:
const flagColor1 = {
color1: "green"
}
const flagColor2 = {
color2: "gold"
}
const flagColor3 = {
color3: "black"
}
const jamaicanFlag = {...flagColor1, ...flagColor2, ...flagColor3}
Here it is combining arrays:
const array = [1,2];
const array2 = [3,4];
const array3 = [...array, ...array2];
array3
[1, 2, 3, 4]
And here it is passing multiple arguments into a function:
const array = [1,2,3];
spreadArgs(...array);
Terminology
For instance, here's a
canEat()
function:const canEat = function(creature) {
const obj = {
eat: function(food) {
return `The ${creature} eats the ${food}.`
}
}
return obj;
}
Here's how we'd use composition to give a
cat
object the ability to eat:> const cat = canEat("cat");
We can use a function factory to add multiple pieces of functionality to an object. For instance, if we wanted to create a creature that can both eat and sleep, we'd do something like this:
const canEat = (creature) => ({
eat: (food) => {
return `The ${creature.name} eats the ${food}.`
}
});
const canSleep = (creature) => ({
sleep: () => {
return `The ${creature.name} sleeps.`
}
});
const sleepingEatingCreature = (name) => {
let creature = {
name
}
return { ...creature, ...canEat(creature), ...canSleep(creature) };
};
Terminology
Terminology
23