Uses self contained pieces of code, (classes in other OOP languages) in JavaScript, functions disguised as classes. Techniques used are Inheritance, Polymorphism, and Encapsulation.
Functional programming uses pure functions and avoids side effects, meaning if you put the same input in, you will always get the same output.
function User(name, email){
this.name = name;
this.email = email;
}
User.prototype.greeting = function(){
return `Hi I am ${this.name}.
Please email me at ${this.emeil}`
}
class User{
constructor(name, email){
this.name = name
this.email = email
}
greeting(){
return `Hi I am ${this.name}.
Please email me at ${this.emeil}`
}
}
const jon = new User('jon', 'jon@me.com')
function createUser(name, email){
const obj = {}
obj.name = name;
obj.email = email;
obj.greeting = function(){
return `Hi I am ${this.name.
Please email me at ${this.email}}`
}
}
let jon = createUser('jon', 'jon@me.com')
let books = [
{name:'JavaScript', pages:450},
{name:'Angular', pages:902},
{name:'Node', pages:732}
];
for (var i = 0; i < books.length; i++) {
books[i].lastRead = new Date();
}Example:
let books = [
{name:'JavaScript', pages:450},
{name:'Angular', pages:902},
{name:'Node', pages:732}
];
let newBooks = books.map((book)=> {
book.lastRead = new Date()
return book;
});
Example:
Master the JavaScript Interview: What is Functional Programming?