-5

I have a question about decalaration of JS function. I declared a function as follows:

create=(name,agfdf,gender)=>({name,a,gender});
(name,agfdf,gender)=>({name,a,gender})

the argument passed is agfdf, but the output does not contain agfdf. The function acutally works:

input: create("P",3,"M")
output: {name: "P", a: 3.6, gender: "M"}
a: 3.6
gender: "M"
name: "P"
__proto__: Object

I do not understand how this would work. Could anyone of you explain? Thanks a lot!

User863
  • 19,346
  • 2
  • 17
  • 41
ZZZ
  • 1
  • 1
  • A verbose way of writing the same code (that might be a more obvious indicator of behavior) is `function create(name, a, gender) { return { name: name, a: a, gender: gender }; }` – nem035 Dec 06 '19 at 05:03

1 Answers1

0

this type of function is called arrow function in js. it is es6 syntax to declare function

let a = ()=>{return 1;}
let b = value=>{return value;}
let c = (value1,value2)=>{return {value1,value2};}

in your js

create=(name,agfdf,gender)=>({name,a,gender});

you are taking 2nd parameter as agfdf but returning a. which might be initialized somewhere in your code

Shirish Maharjan
  • 502
  • 3
  • 17
  • thank you for your answer. If I had not initialized a before this declaration, it would generate an error right? – ZZZ Dec 06 '19 at 07:28