user12700367
user12700367

Reputation: 69

A better way to send the arguments as an object literal?

I have this function code that I am using to create a code snippet in javascript. I want to know if I can make it look better since I think this code I did is not looking professionally well.

I would like to know if there is any better way of passing multiple parameters to a function in javascript sending the arguments as an object literal?

Many thanks.

let headings = document.getElementById('headlines');

function create(listLi, txt, p) {
  let li = document.createElement(listLi)
  li.innerHTML = txt;
  p.appendChild(li);
  return li;
}

let ul = create('ul', null, document.body);

Upvotes: 0

Views: 148

Answers (1)

Barmar
Barmar

Reputation: 781741

You can use destructuring.

function create({listLi, txt, p}) {
  let li = document.createElement(listLi)
  li.innerHTML = txt;
  p.appendChild(li);
  return li;
}

let ul = create({
  listLi: 'ul',
  txt: null,
  p: document.body
});

Upvotes: 2

Related Questions