This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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! | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //immediately prints the value 'welcome to the jungle' | |
| (function(){ | |
| return "Welcome to the jungle!"; | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //syntax for creating an IIFE | |
| (function () { | |
| //functions statements go here | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 |