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

Function Parameters Question

I am working through the build a to do app with JS project and noticed something interesting from the instructor.

He seems to be using the parameter names as an object. For example below taskListItem and checkBoxEventHandler are only parameter names, they do not exist anywhere else in the program as variables yet he is able to access object properties and methods with them such as querySelector.

var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
  console.log('bind list item events');

  var checkBox = taskListItem.querySelector('input[type=checkbox]');
  var editButton = taskListItem.querySelector('button.edit');
  var deleteButton = taskListItem.querySelector('button.delete');

  editButton.onclick = editTask;
  deleteButton.onclick = deleteTask;
  checkBox.onchange = checkBoxEventHandler; 
}

for (var i = 0; i < incompleteTasksHolder.children.length; i++) {
  bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
}

for (var i = 0; i < completedTasksHolder.children.length; i++) {
  bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
}

I was unaware that you could perform methods() or check properties using a parameter name. To clarify, is that whats going on here?

1 Answer

Looks like you can manipulate parameters in this manner. News to me, for anyone reading eager for an answer as well, look at this example:

var dogVar = 'dog';
var catVar = 'cat';

function stringCheck (string, string2) {
  console.log(string.length + string2.length);
}

stringCheck(dogVar, catVar);