Foundations Flashcards
(36 cards)
Object.getPrototypeOf()
Returns the object’s prototype property
Parameters:
Takes an object
Returns:
Value associated with the [[Prototype]] property which is all of the built-in prototype methods
Object.create()
Creates an object
Const obj = Object.create(someObj)
Adds one object to another objects __proto__
In the example above someObj is added as a method to the obj’s prototype chain
This. keyword
Refers to the object scoped above as the owner of the property that comes after the dot in the variable reference
Factory function
A function that
- Returns an object literal
- Is not a constructor function
Example
Function puppyFactory(name, breed) { Return { Name, Breed } }
Const Fido = puppyFactory(“Fido”, “bulldog”)
Object creational design pattern
Design pattern that instantiates and organizes the object for what it will be used for later
Arrow function
Syntactically compact alternative to a regular function expression
Ill-suites as methods
Cannot be used as constructor functions
Regular functions:
Function func() { Return { Name, Age } }
Function func() { Return { Name, Age } }
Constructor functions
Ordinary functions invoked with the new operator and return an object instance
First letter is capitalized
Const Jedi = function(level, color) {
Const instance = {level,color}
Return instance
}
Const newInstance = new Jedi(‘Luke’, ‘green’)
New keyword
Allows the user to create a new object using a constructor function
Const sneaker = new Shoe()
Console.dir
Console.log but shows all properties attached to an object
This list is interactive which means that it’s possible to see all the nested properties of an object like the .prototype and .__proto__ properties
arguments
An array-like object that contains all the arguments passed into a function
function func(a) {
Console.log(arguments[0])
}
A
.forEach method
takes a function as an argument and Runs that function on each element in an array
This method is called on an array
Arr.forEach(function)
———
For (let i = 0: i < arr.length; i++) { Copy.push(items[i]) }
items.forEach(function(item) {
Copy.push(item)
}
Refactor with arrow function:
Hello = function() {
Return “hello”
}
Hello = () => {
Return “hello”
}
Arrow function that returns an object
Const funcname = (parameters) => ({key: value, key: value})
ForEach with arrow function notation
forEach.((func, index, arr) => {
Console.log(“hello”)
}
Spread operator
…
Refers to name of array in place of its elements
Const numbers = [1,2,3]
Console.log(…numbers)
1,2,3
Behind the scenes it creates a shallow copy of the array
Cannot he used with nested objects or arrays
First class functions
Returned by another function
Passed in as an argument
Assigned as a variable, method or stored in an array
Higher order functions
Takes another function as an argument
Filter method
Const checkAdult = (age) => {
Return age >= 18
}
Const func = () => {
Let adultsNumber = Ages.filter(checkAdult)
Return adultNumber
}
Map method
Arr.map(function)
Returns an array
Similar to forEach, but map returns something (an array
Map method chain
Add one method after another separating them with a dot. This causes the methods to be called one after the other
myName = “Scott”
myName.toUpperCase()
SCOTT
myName.toUpperCase().split(‘’)
[S,C,O,T,T]
Object.keys() and Object.values()
Returns all keys and values respectively as an array
Map method with an object
Const someVar = Object.keys(petObj).map((key) => {
Return ${key} is a petObj[key]
}
Reduce method
Accepts a function and a starting value as arguments
Returns a single value
Arr.reduce(callback,initial value)
Accumulator = the value that we end with
Reducer = the action we perform on each value in the iterable object to get the result
Const initialvalue = 0
Const numbers = [5,10,15]
Const reducer = (accumulator, item) => {
Return accumulator + item
}
Const total = numbers.reduce(reducer,initial value)
.call method
Recontextualizes the ‘this’ keyword to another object.
Call takes an object as an argument and the this redirects itself to refer to the values in that object
Const person = {
fullName: function() {
Return this.firstName + this.lastName
}