I saw some good function rewrites, and i wanted to share my way too maybe it can help someone as well, i actually expanded what @bigpreshy did on his function, as Anonymous function so i went a bit more further, doing two things.
I made a Function Declaration., for the person function.
I output the result with concatenate template strings. (since you guys are using Vetics/template literals ). I was thinking of using or rewritten that as well as another alternative.
function person (name){
// function Declaration.
function sayName (){
console.log('my name is ' + name);
};
return sayName();
};
@ShAfi@bigpreshy@imendieta Really appreciate all the solutions given by you all. I converted those arrow functions into regular functions. However Im getting this error on ( 1 ). I did some mistake with return or that ( 1 ) in my code.
VM290:18 Uncaught TypeError: Cannot read property ‘sayName’ of undefined
at :18:6
(anonymous) @ VM290:18
I did some mistake maybe with return or that ( 1 ) in my code.
Can anyone please fix this code. Thank you.
function Person (name) {
function sayName () {
console.log(`my name is ${name}`)
}
return sayName()
}
function Nerd (name) {
// simply create a person and pull out the sayName function!
const sayName = Person(name) // . . . . . . . ( 1 )
function doSomethingNerdy (){
console.log('nerd stuff')}
return sayName;
}
const jeff = Nerd('jeff')
jeff.sayName() //my name is jeff
jeff.doSomethingNerdy()
@bigpreshy so it seems he wanted to archive the same two results of
//my name is jeff
//nerd Stuff
but rewrite as regular functions. I came up with this, remember @jainashish0712 that you can also do function declaration, and get the same results.
Note: Your getting error of SayName its not a function or sayName of undefined.
because your returning sayName instead of doSomethingNerdy.
function person (name){
// function Declaration.
function sayName (){
console.log('my name is ' + name);
};
return sayName();
};
const Nerd = function (name){
// simply create a person and pull out the sayName function!
const sayName = person(name) // . . . . . . . ( 1 )
// function declaration
function doSomethingNerdy (){
console.log('nerd stuff');
}
return doSomethingNerdy();
};
const jeff = Nerd('jeff');
// output:
// my name is jeff
//nerd stuff
Note: on my code again i made a function declaration, you can also do it with anonymous function like you have it but returning doSomethingNerdy();.
hope this helps.