Created
May 2, 2019 01:28
-
-
Save deconstructionalism/40f5f31cb26199235e77b9a9f8dd4376 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
named functions inside objects are called methods | |
*/ | |
function returnAnObject(name, age) { | |
const myObject = { | |
myName: name, | |
myAge: age, | |
sleep: function() { | |
console.log('zzzzz') | |
} | |
} | |
return myObject | |
} | |
const arjun = returnAnObject('Arjun', 34) | |
// { myName: 'Arjun', myAge: 34, sleep: [function]} | |
arjun.myName | |
// 'Arjun' | |
arjun.myAge | |
// 34 | |
arjun.sleep | |
// [Function] | |
arjun.sleep() | |
// 'zzzzz' | |
/* | |
how to update an Object that is passed in as an argument | |
to a function | |
*/ | |
function updateObject(input) { | |
// const input = {name: 'Arjun', age: 34} | |
input.name = 'Bethany' | |
input.foo = 'foo' | |
input.bar = 'bar' | |
input.pow = 'wow' | |
return input | |
} | |
updateObject( {name: 'Arjun', age: 34} ) | |
// {name: 'Bethany', age: 34, foo: 'foo', bar: 'bar', pow: 'wow} | |
/* | |
when you pass in data to a function when running it, that data | |
is provided inside the function block (stuff inside these {}) | |
as a local variable, named whatever it was named when you made | |
the function | |
*/ | |
function sayName(name) { | |
// const name = 'Bethany' | |
console.log(name) | |
} | |
sayName('Bethany') | |
/* | |
Functions should take it ALL the data they need as input arguments. | |
They should almost never rely on variables that were set outside of | |
the function, because those variable can change in value, and then | |
your function ends up having multiple different results even with | |
the same input arguments. This is BAD! for any given set of input args | |
the function should have one and only one consistent output for each | |
set | |
*/ | |
function add (num1, num2) { | |
return num1 + num2 | |
} | |
add(4, 6) | |
// 10 | |
// BAD | |
let number = 6 | |
function addThreeToNumber () { | |
number + 3 | |
} | |
addThreeToNumber() | |
console.log(number) | |
// 9 | |
addThreeToNumber() | |
console.log(number) | |
// 12 | |
addThreeToNumber() | |
console.log(number) | |
// 15 | |
// GOOD | |
function addThreeToNumber (thisNumber) { | |
return thisNumber + 3 | |
} | |
addThreeToNumber(6) | |
// 9 | |
addThreeToNumber(6) | |
// 9 | |
addThreeToNumber(10) | |
// 13 | |
/* | |
how to deal with object keys with spaces in them. Don't do this | |
yourself, but you may have to deal with this if you are working | |
with other people's data | |
*/ | |
const dog = { | |
name: 'Bow wow', | |
breed: 'Tibetan Mastiff', | |
dateOfBirth: '2019/04/01', | |
sizeOfPaw: 'large', | |
'badly name key': 'BAD' | |
} | |
dog.name | |
// 'Bow wow' | |
dog.dateOfBirth | |
// '2019/04/01' | |
dog['name'] | |
// 'Bow wow' | |
dog.badly named variable | |
// ERROR THIS DOES NOT WORK | |
dog['badly named variable'] | |
// 'BAD' | |
/* | |
String interpolation / template strings (strings that use ``) | |
` - are called backticks | |
they also allow for multi-line string! | |
allows more elegant insertion of JS into string | |
*/ | |
const name = 'Bethany' | |
const favoriteColor = 'brown' | |
console.log('my name is ' + name + ' and my favorite color is ' + favoriteColor) | |
// 'my name is Bethany and my favorite color is brown | |
console.log(`my name is ${name} | |
and my favorite color is ${favoriteColor} | |
and one plus one is ${1+1}`) | |
// my name is Bethany | |
// and my favorite color is brown | |
// and one plus one is 2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment