#Make a method with a lambda parameter, WITH a argument

1 messages · Page 1 of 1 (latest)

quartz notch
#

Hello.

I am making a method. I would like to be able to call this, such as method(arg => { some code})

I've heard the => is called lambda. I am googling how to make a method that takes a lambda as a parameter, while the lambda also having its own arguments.

It seems nobody on the internet knows how to do this because this all just looks unreadable and wrong.

none of these make sense. They do not look right. that is not a method. Why is there a const. Why are you setting it equals.

How on gods green earth do these people do this.

#

Attention. I have figured it out

#
method(lambda: ((paramName: paramType) => void)):void
{
 //Your code.
}
#

pin this

#

i beg you pin this

#

people cant go through this suffering

zinc onyx
#

Function defined with => are often called arrow functions or fat arrow functions

#

But the (arg: ArgType) => ReturnType format is used for all functions in TypeScript

#

You would also be able to call your method like

obj.method( function('foo') {} )

the difference between function and => is that array functions can access the this variable of the surrounding scope, when mostly TypeScript doesn't care about this when creating function types (technically you can require a certain this for the function with (this: ThisType, arg: ArgType) => ReturnType )

#

So if you understand the basics, you know that you just need to define an argument with a function type

#

The handbook explains function types here:

#

Also the term callback is used a lot to talk about functions that are passed to other functions

#

I think the first result would explain it

#

I guess searching for lambda doesn't give good results. Partly thanks to AWS Lambda stealing some spots.

#

@quartz notch

quartz notch
#

also yeah i thought of callback but i come from c# and i just kinda had that feeling that "callback" isnt a typescript term

zinc onyx
#

Ah yeah I was thinking lambda is more of an other language term. The term is used in Kotlin I think, C# too?

JavaScript is a bit different to some other languages. Traditionally functions are static and can't be passed as arguments to functions. I think C# has no standalone functions, just methods, and I guess methods can't be passed as arguments? But you can define delegates that can? So suppose these languages might need a separate concept of lambdas for quick anonymous function that can be passed as arguments.

But in JavaScript all functions are just special objects that can be passed as arguments, and all functions can be anonymous, so this concept has been there from the beginning. Callback coding was very heavily used in the earlier days of JavaScript before we had promises and async/await. So the new => syntax was introduced just to change the way the this variable was handled, but other than that they are essentially the same as other functions. So yeah I think we mostly talk about callbacks and functions rather than lambdas.

"Arrow function" seems to be the official term too: https://tc39.es/ecma262/multipage/ecmascript-language-functions-and-classes.html#sec-arrow-function-definitions

quartz notch
#

arrow function is a very like

#

sorta dumb way to say it lol but it makes a lot more sense

zinc onyx
#

Yeah its pretty literal. Doesn't sound as call as lambda function haha

quartz notch
#

yeah LOL

neat seal
#

"lambda function" is a term imported from math, it's used in java, c#, c++, python, to my knowledge. it's just js that doesn't use the term

#

they effectively do the same though