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 Object-Oriented JavaScript (2015) Introduction to Methods Adding a Method to an Object

how to create an anonymous function in a code like the one below

how to create an anonymous function in a code like the one below, help

6 Answers

Kelly von Borstel
Kelly von Borstel
28,880 Points

Hi, Maxwell.

To create an anonymous function, use the keyword 'function' but don't follow it with a function name.

The contact object needs a property 'fullName' that is an anonymous function — this is considered a method (what we call functions that are properties of objects). Here's part of the answer... where I left question marks is where you want to place the same code from the printFullName function into the body of the anonymous function.

var contact = {
  fullName: function() {
    ???
    ???
    ???
  }  
}

it was quite helpful, thanks!!

what u need to do is to write: fullName: function(){} inside the var contact{} then take the code from line 2,3& 4 as well as the curly braces(opening and closing).for me,this code is running well,try it : var contact = { fullName: function() { var firstName = "Andrew"; var lastName = "Chalkley"; console.log(firstName + " " + lastName); }
} task 2of2 u just click check work if u have passed that code above.this is so because it requires u to create an anonymous function of which we have done that already on when we wrote the code fullName: function(){} if it fails to work,send a notification with the code u are trying. happy coding...

hey Kelly

it keeps saying bummer! expected the printFullName function.

Maxwell may you please help as well.

Hie Noku,just paste this code on the first challenge: var contact = { fullName: function() { var firstName = "Andrew"; var lastName = "Chalkley"; console.log(firstName + " " + lastName); }
} Hope it helps & if it does so don't forget to mark my answer as best

it keeps saying bummer Maxwell, do l have to keep the code l found on the challenge nd just paste the one on the forum underneath it OR l have to replace the code l found on the challenge with the new one??? m so frustrated my deadline is fast approaching.

it worked this time...funny though cause its been the same code l have been using. Thanks a lot for your patience. See you at graduation.