What are the benefits of having a function take in another function?
function findOdds(arr){
let temp = []
for(let i = 0; i < arr.length; i++){
if (arr[i] % 2 !== 0) temp.push(arr[i])
}
return temp
}
findOdds([1,2,3,4]) // [1, 3]
function findNamesLongerThanFive(arr){
let temp = []
for(let i = 0; i < arr.length; i++){
if (arr[i].length > 5) temp.push(arr[i])
}
return temp
}
findNamesLongerThanFive(['Bryan', 'Charlotte', 'Lindsey', 'June']) // ['Charlotte', 'Lindsey']
They take in functions as parameters
Is this a decorator?
Are these decorators?
Is this a decorator?
@doSomething
For the love of everything holy, Bryan...Yes these are all "decorators"
- You
function readOnly(target, name, descriptor){
descriptor.writable = false
return descriptor
}
To find out what properties are available, we conveniently can use Object.getOwnPropertyDescriptor
const me = {
name: 'Bryan',
age: 29,
tattoos: true,
family: ['Lindsey', 'Coco', 'June']
}
Object.getOwnPropertyDescriptors(me)
function readOnly(target, name, descriptor){
descriptor.writable = false
return descriptor
}
class User {
constructor(name) {
this.name = name;
}
@readOnly
greet() {
return this.name + ' says hello'
}
}
This makes it so no one can come in and hug stuff up like this:
User.prototype.greet = function(){
return this.user = " loves to rollerblade"
}