Я видел, как некоторые веб-сайты говорили о том, что есть лучший способ использования параметров в функции, а также что объектный литерал - это хороший способ сделать код лучше и быстрее.
Я пытался измениться, используя деструктуризацию, но не получилось.
У меня есть этот код функции, который я использую для создания фрагмента кода в javascript. Я хочу знать, смогу ли я сделать его лучше, потому что я думаю, что этот код не выглядит профессионально.
const create = function (t, v, p) {
let n = document.createElement(t);
n.innerHTML = v;
p.appendChild(n);
return n;
};
// create the list for reference
let ul = create('ul', null, document.body);
вот полный JS код
const createsnippets = function (e) {
// the reference node -- we atually have one ID for headlines
let headlines = document.getElementById('headlines');
// utility function to add new HTML DOM element
const create = function (t, v, p) {
let n = document.createElement(t);
n.innerHTML = v;
p.appendChild(n);
return n;
};
// create the list for reference
let ul = create('ul', null, document.body);
// find all newsarticle classess and add then to snippet in sequence
Array.from(document.querySelectorAll('article.newsarticle > h4')).forEach(function (h4) {
create('li', h4.textContent + ' ... ' + h4.nextElementSibling.innerText, ul);
});
// insertion re-factors HTMl to ensure the layout (markup and CSS) when displaed
headlines.parentNode.insertBefore(ul, headlines.nextSibling)
}
// once prepared, we can display the loaded content as snippet/collection
document.addEventListener('DOMContentLoaded', createsnippets);