Skip to content

Instantly share code, notes, and snippets.

View Maccauhuru's full-sized avatar
🎯
Learning Everyday

Simba Mupfukudzwa Maccauhuru

🎯
Learning Everyday
  • Software Developer at Clear Technologies
  • Dallas
View GitHub Profile
@Maccauhuru
Maccauhuru / script.js
Last active January 19, 2019 17:57
Example of An IIFE
//immediately prints the value 'welcome to the jungle'
(function(){
return "Welcome to the jungle!";
})();
@Maccauhuru
Maccauhuru / script.js
Last active January 19, 2019 20:43
IIFE Variable Assignment Example
//This variable holds/stores a function that prints 'welcome to the jungle'
const welcomeMessageVariable = function (){
return "Welcome to the jungle!";
};
//we will invoke our first variable here below
welcomeMessageVariable(); // Welcome to the jungle!
//This second variable holds/stores the value 'welcome to the jungle'
@Maccauhuru
Maccauhuru / script.js
Created January 19, 2019 21:17
Example of IIFE with variables and parmeters
const greetingMessageIIFE = (function(name,message){
let headline = `Lets create some IIFE's`;
return `${headline}
Hello ${name},${message}`
})('Neo',"Welcome to the Matrix!");
//Lets create some IIFE's
//Hello Neo,Welcome to the Matrix!
@Maccauhuru
Maccauhuru / script.js
Created January 21, 2019 03:25
Impure Function Example
//This function is 'impure' and is not re-usable
function addNumbers(){
let num1 = 100;
let num2 = 1;
return num1 + num2;
}
addNumbers();//will always return 101
@Maccauhuru
Maccauhuru / script.js
Last active January 28, 2019 02:39
Example Object Definition
//lets define an object called book
const book = {};
//lets add two properties to our book object
book.name = "Eloquent JavaScript: A Modern Introduction to Programming";
book.author = "Marijn Haverbeke"
//lets define a method to get the book details
book.getBookDetails = function (){
return "The book " + this.name + " was written by " + this.author;
@Maccauhuru
Maccauhuru / script.js
Last active January 28, 2019 03:46
Example Object Constructor Function
//define Book as an object constructor function
function Book(name,author) {
this.name = name;
this.author = author;
this.getBookDetails = function (){
return "The book " + this.name + " was written by " + this.author;
}
}
//add some new books
@Maccauhuru
Maccauhuru / script.js
Created February 11, 2019 01:11
Object Method ES5
//define Book as an object constructor function
function Book(name,author) {
this.name = name;
this.author = author;
}
//add getBookDetails method to object prototype
Book.prototype.getBookDetails = function (){
return `The book ${this.name} was written by ${this.author}`;
}
@Maccauhuru
Maccauhuru / script.js
Last active February 26, 2019 14:27
ES6 Classes Example
//define Book as an object constructor function
class Book {
constructor(name,author,price){
this.name = name;
this.author = author;
this.price = price;
}
getBookCost(){
return `The book ${this.name} was written by ${this.author} and costs $${this.price}`;
}
@Maccauhuru
Maccauhuru / script.js
Created February 27, 2019 01:32
Getters & Setters Example 1
const movie = {
name : "The Godfather",
year : 1972,
director: 'Francis Ford Coppola',
imdb_Rating: 9.2,
get movieDetails(){
return `
The movie : '${this.name}' was directed by ${this.director} and released in the year ${this.year}.
It has a very high IMDB Rating of ${this.imdb_Rating} / 10.
`
@Maccauhuru
Maccauhuru / script.js
Last active February 27, 2019 02:15
Getters & Setters Example 2
const movie = {
name : "The Godfather",
year : 1972,
director: 'Francis Ford Coppola',
imdb_Rating: 9.2,
set actors(name) {
this.cast.push(name);
},
cast: [],
get movieDetails(){