<aside> ‼️ REMINDER: A Higher Order Function is a function that takes in a function as an argument or returns function.
</aside>
// ES6 Arrow functions.
const myFunction = (param1, param2) => {
// do code here
}
// ES5 - traditional method
function myFunction(param1, param2) {
// do code here
}
A function that returns an object. It’s an object creator. We use them a lot in JavaScript and they lend themselves to a paradigm called: ‘Object Oriented Programming’. CordialPerson
will be a function that creates an object for us. When we call it we have to use the new
keyword.
Example:
function CordialPerson(greeter) {
this.greeting = 'Hello ';
this.greeter = greeter;
this.speak = function() {
console.log(this.greeting + this.greeter);
console.log(this);
};
}
const jerry = new CordialPerson('Newman');
const newman = new CordialPerson('Jerry');
jerry.speak();
newman.speak();
Example 2:
function Person(attributes) {
this.age = attributes.age;
this.name = attributes.name;
this.homeTown = attributes.homeTown;
this.speak = function () {
return `Hello, my name is ${this.name}`;
};