Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript DOM Scripting By Example Improving the Application Code Refactor 1: Create List Items

const createElement = (elementName,property,value) =>{ const element = document.createElement(elementName);

My question with this is under the "const element" portion, in the example Guil used

        const createElement = (elementName,property,value) =>{
              const element = document.createElement(elementName);
              element[property] = value;

              return element;

if I want to use the function above to refactor this code below, why are we not using a parameter and argument for "editButton" to be passed in the createElement above?

        const editButton = document.createElement("button");
              editButton.textContent  = "Edit";

why is it like this instead

const editButton = createElement("button","textContent","Edit");

1 Answer

Charlie Prator
.a{fill-rule:evenodd;}techdegree seal-36
Charlie Prator
Front End Web Development Techdegree Graduate 19,854 Points

I'm not sure if this is correct, but I believe it's because you don't need these variables outside the function and can't access them anyway.

In the first iteration, we created the "editButton" variable, which stores the newly created edit button, set its value to "Edit", and append it to our <li>. And, that's it. We're done with the variable "editButton". It has no purpose beyond the creation of that element.

In the DRYer code, the variable name doesn't matter. It just holds the content of what we created, which is now in the DOM ready to use.