Arrow function: how to rewrite to regular function?

Can anyone please help I want to rewrite all arrow functions in this code into regular functions. A little explanation on ( 1 ) would be helpful.

const Person = (name) => {
  const sayName = () => console.log(`my name is ${name}`)
  return {sayName}
}

const Nerd = (name) => {
  // simply create a person and pull out the sayName function!
  const {sayName} = Person(name) // . . . .  . . . ( 1 )
  const doSomethingNerdy = () => console.log('nerd stuff')
  return {sayName, doSomethingNerdy}
}

const jeff = Nerd('jeff')

jeff.sayName() //my name is jeff
jeff.doSomethingNerdy() // nerd stuff

How I would break it up:

Original:

const Person = (name) => {
  const sayName = () => console.log(`my name is ${name}`)
  return {sayName}
}

First, I’ll remove the const keyword and the equal sign:

Person (name) => {
  const sayName = () => console.log(`my name is ${name}`)
  return {sayName}
}

Then add the function keyword:

function Person (name) => {
  const sayName = () => console.log(`my name is ${name}`)
  return {sayName}
}

and finally I’ll remove the fat arrow:

function Person (name)  {
  const sayName = () => console.log(`my name is ${name}`)
  return {sayName}
}

That’s how you transform an arrow function into a regular function :sunglasses:

3 Likes

This works too.
I am going to use the expressive way of writing function:


const Person = function(name){
   const sayName = function(){
   console.log(`my name is ${name}`);
   
}
return sayName();
}

Understand that arrow function is a feature of ES6 and it is meant for you to write shorter and cleaner code.

By default, arrow function has implicit return:
That is to say that you don’t necessarily need to use the key word “return”

1 Like

Hi there,

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.

  1. I made a Function Declaration., for the person function.
  2. 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();
   
};

Hope this help as well.

1 Like

@imendieta Good too.
:+1:

1 Like

@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()

What are you trying to achieve?

Hi,

@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.

1 Like