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 trialKota Fukada
16,243 Pointsfunction(taskListItem, checkBoxEventHandler) How come "taskListItem" and "checkBoxEventHandler" work?
var bindTaskEvents = function(taskListItem, checkBoxEventHandler){
console.log("Bind list item events");
//select taskListItem's children
var checkBox = taskListItem.querySelector("input[type=checkbox]");
var editButton = taskListItem.querySelector("button.edit");
var deleteButton = taskListItem.querySelector("button.delete");
//bind editTask to edit button
editButton.onclick = editTask;
//bind deleteTask to delete button
deleteButton.onclick = deleteTask;
//bind checkBoxEventHandler to checkbox
checkBox.onchange = checkBoxEventHandler;
}
I thought "taskListItem" and "checkBoxEventHandler" are just name of function. Why they have some elements? Because they are used as "taskListItem.querySelector();" and "checkBox.onchange = checkBoxEventHandler".
I know I don't understand how "function()" work. I only know "taskListItem" and "checkBoxEventHandler" are param.
1 Answer
Steven Parker
231,275 PointsIn JavaScript, functions are "first class" objects. This means that they can be handled like any variable.
So function names can be supplied as event handlers, called with parameters, or be passed as parameters. You were right on both counts. The concepts are not contradictory.
On the other hand, "function(param1, param2)" is known as an "anonymous function". It cannot be called directly because it has no name, but it still can be passed as a parameter, and take its own parameters. In fact, anonymous functions are normally used only when they are passed as parameters and not needed anywhere else.
Kota Fukada
16,243 Pointsthank you Then could you explain me when "taskListItem" used as var checkBox = taskListItem.querySelector("input[type=checkbox]");
as a example, how "taskListItem" works??
Ashish Mehra
Courses Plus Student 589 PointsAshish Mehra
Courses Plus Student 589 Pointshay kota ,check out below code which help u to understand how function working
i think this help you to understand how function working
thanx