Skip to content

Instantly share code, notes, and snippets.

@suhailgupta03
Created August 8, 2023 15:39
Show Gist options
  • Save suhailgupta03/bc645ce032dd57a986ed28a553d94d77 to your computer and use it in GitHub Desktop.
Save suhailgupta03/bc645ce032dd57a986ed28a553d94d77 to your computer and use it in GitHub Desktop.
/**
* 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