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 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
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
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
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
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
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 17:09
IIFE Syntax
//syntax for creating an IIFE
(function () {
//functions statements go here
})();
@Maccauhuru
Maccauhuru / script.js
Last active January 16, 2019 10:41
ES6 Arrow Functions More Examples
//Arrow function that uses an implicit return
const addNumbers = (num1,num2) => num1 + num2;
addNumbers(10,20); // 30
//Arrow function that uses a 'return' keyword in the block body
const addSomeNumbers = (num1,num2) => {
let total = num1 + num2;
return total;
}
@Maccauhuru
Maccauhuru / script.js
Created January 16, 2019 10:14
Example of Arrow Function 2
//Arrow function that takes any number as input
//adds 100 and returns the total
const addHundred = num => num + 100;
addHundred(1);//output : 101
addHundred(1000); //output : 1100
@Maccauhuru
Maccauhuru / script.js
Created January 16, 2019 09:44
ES6 Arrow Functions Example
//An ES6 Arrow function named addNumbers that accepts 2 input parameters
const addNumbers = (num1,num2) => {
let sum = num1 + num2;
return sum;
}
//To invoke the function,i will call its name (addNumbers) and give it 2 parameters
addNumbers(5,2); // will output : 7
addNumbers(100,1); // will output : 101