This is one of the weird cases where it is actually easier to show with TypeScript:
// A type of function that takes a // `string` argument and returns `undefined`typeChildFn=(name:string)=>void;// A type of function that takes a// `string` argument and returns a `ChildFn`typeParentFn=(name:string)=>ChildFn;// `create` is a function of type `ParentFn`// i.e. it takes a string argument and returns// a `ChildFn` function. constcreate:ParentFn=(parentName)=>(childName)=>print(`${parentName}${childName}`);// i.e. `create` returns the function // `(childName) => print(`${parentName} ${childName}`)`// note that `parentName` comes from the closure that // created the function.// The function returned from `create` // is bound to `child` and has access // to `parentName = 'Bitcoin' // through its closure which created itconstchild:ChildFn=create('Bitcoin');// Here we finally supply `childName` // so that the full string is logged.child('Dogecoin');// ---functionprint(...args:any[]):void{console.log(...args);}
So it's at child('Dogecoin'); that we supply childName.
// A type of function that takes a// `string` argument and returns `undefined`typeChildFn=(name:string)=>void;// The function value returned from `create`// is bound to `child` and has access// to `parentName = 'Bitcoin'// via the closure that created itconstchild:ChildFn=create('Bitcoin');// Here we finally supply `childName`// so that the full string is logged.child('Dogecoin');// [LOG]: "Bitcoin Dogecoin"// ---// `create` is a function that takes// a `string` argument and returns// a function of the `ChildFn` type.functioncreate(parentName:string):ChildFn{returnchildFn;// ---functionchildFn(childName:string):void{print(`${parentName}${childName}`);}}// i.e. `create` returns the `childFn` function// value which has access to `parentName`// value via the closure that created it.functionprint(...args:any[]):void{console.log(...args);}
I believe it's a bit more difficult to understand.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In the Closure example
The parameter "childName", where will it get its value from?
I'm new to js and this issue is confusing me
This is one of the weird cases where it is actually easier to show with TypeScript:
So it's at
child('Dogecoin');
that we supplychildName
.A version that uses function declarations only:
I believe it's a bit more difficult to understand.