Skip to content

Instantly share code, notes, and snippets.

@AlexMercedCoder
Created April 17, 2021 13:52
Show Gist options
  • Save AlexMercedCoder/a2b04d3421550dbe9ebbbb06ad70a5d7 to your computer and use it in GitHub Desktop.
Save AlexMercedCoder/a2b04d3421550dbe9ebbbb06ad70a5d7 to your computer and use it in GitHub Desktop.
Nest Data Structures in Javascript
///////////////////////////////
// Array of Objects
//////////////////////////////
console.log("------------------------------------")
const dogs = [
{name: "Spot", age: 6},
{name: "Fluffy", age: 6},
{name: "Clifford", age: 6},
{name: "Lassi", age: 6},
]
console.log(dogs)
console.log(dogs[1])
console.log(dogs[1].name)
console.log("------------------------------------")
///////////////////////////////
// Functions that Return Objects
//////////////////////////////
console.log("------------------------------------")
const dogGen = (name, age) => {
return {
name,
age,
treats: ["bacon", "salmon"]
}
}
console.log(dogGen)
console.log(dogGen("Fluffy", 5))
console.log(dogGen("Fluffy", 5).name)
console.log(dogGen("Fluffy", 5).treats[1])
console.log("------------------------------------")
////////////////////////////////////
// Array of Functions
////////////////////////////////////
const funcArray = [
() => console.log("I work"),
() => () => console.log("I'm a function returned by another function")
]
console.log(funcArray)
funcArray[0]()
console.log(funcArray[1]())
funcArray[1]()()
//////////////////////////////////
// Array Classes
//////////////////////////////////
const Wizards = [
() => class {
name = "Bob"
},
class {
name = "Steve"
}
]
const wiz = Wizards[0]()
const Bob = new wiz
const Steve = new Wizards[1]
console.log(Bob)
console.log(Steve)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment