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 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
seal-mask
.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.