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

Gremyko Coleman
Gremyko Coleman
Pro Student 6,006 Points

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");
Charlie Prator
Charlie Prator
Front End Web Development Techdegree Student 11,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.