Created
August 8, 2023 15:39
-
-
Save suhailgupta03/bc645ce032dd57a986ed28a553d94d77 to your computer and use it in GitHub Desktop.
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 name = 'Rex'; | |
const age = 2; | |
const color = "brown"; | |
function description() { | |
console.log( | |
`This is ${name}, he is ${age} years old and his color is ${color}` | |
) | |
} | |
description(); | |
// This is an example of functional programming. | |
// The function description is a pure function. | |
// It does not depend on any external variables. | |
// It only uses the variables that are passed | |
// to it as arguments. This is a good practice. | |
// It makes the code more readable and easier to maintain. | |
// It also makes the code more testable. | |
*/ | |
function Dog() { | |
const name = 'Rex'; | |
const age = 2; | |
const color = "brown"; | |
function description() { | |
console.log( | |
`This is ${name}, he is ${age} years old and his color is ${color}` | |
) | |
} | |
function makeSound() { | |
if (age < 2) { | |
console.log("barf .. barf"); | |
} else { | |
console.log("woof .. woof"); | |
} | |
} | |
return { | |
description: description, | |
makeSound: makeSound | |
}; | |
} // returns an object | |
const myDog = Dog(); // call to Dog returns an object; hence myPet is an object | |
myDog.description(); | |
myDog.makeSound(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment