Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Hi There, im a bit confused by the used syntax in this tutorial. Are there more types of anonymous functions ?

Hi There, im a bit confused by the used syntax in this tutorial

Q: Are there more types of anonymous functions ?

Function Expression aka anonymous function I thought i learned in this course the anonymous function https://teamtreehouse.com/library/function-declarations-vs-function-expressions

const x = function (a, b){};

in this course (Source (A), see below. ) about callbacks functions, they also mention a type of anonymous function.

Q:im i understanding it correctly the there are more types of anonymous function ?? 1.Function Expression= anonymous function 2.pasting the anonymous function over the sayHello name.1:11 When writing a function like this, this is also know as inlining or1:16 writing a function in line.1:21

I googled a bit an d found some more info, see below source (b)

can someone help me out??

thanx. greets Max

////////////////////////////////// source (A) https://teamtreehouse.com/library/anonymous-functions-2

First, I can change my sayHello function from a function0:28 declaration to a function expression (1) by adding a variable,0:32 sayHello, And deleting the name of the function.

const sayHello= function() { console.log('Hello'); }

Now to convert this function to an anonymous function (2) ,0:46 I can just get rid of the variable name.0:50

function() { console.log('Hello'); }

How do we pass the anonymous function into the executeCallback without a reference?0:53 The function doesn't have a name anymore.0:59 We can write it in place of the sayHello name.1:02 We can do that by cutting and1:06

function executeCallback ( callback) { callback(); }

executeCallback(function() { console.log('Hello'); });

pasting the anonymous function over the sayHello name.1:11 When writing a function like this, this is also know as inlining or1:16 writing a function in line.1:21 It can be more expressive.

////////////////////////////////////

Source (B): https://medium.com/@soyoung823/inline-function-expression-d44e6f53ce40

A function expression is when a function is assigned to a variable. And, in JavaScript, this can also happen when you pass a function inline as an argument to another function.

1 Answer

Charlie Palmer
Charlie Palmer
15,445 Points

Response to your question. These are ways you can write methods/functions in JS. function myFunction(a+b){return a+b}; const myFunction = (a,b) => {return a+b}; (a,b)=>{return a+b};

I'm not too sure what you are asking about the Callback method. I can try to explain to you what it is doing.

functions executeCallBack( callback ) {
  callback();
}

This method is defined and allows for one parameter to be passed in. In the executeCallback() method it uses the parameter as a method, it means that the parameter passed in is expected to be a function/method.

This means you can pass in a function to the callback method by either of the following ways:

executeCallback(() => {console.log('Hello World')}); //Hello World

or

const helloWorld = () => console.log('Hello World');
executeCallback(helloWorld); //Hello World

or

function concatMyString(a,b) {return a+' '+b};
executeCallback(concatMyString('Hello', 'World')); //Hello World

I hope this helped. If you want anything more specific then please just ask.

Hi Charlie, thank you for your replay. it helped me to get it more clarified!