Skip to content

Instantly share code, notes, and snippets.

@phwb
Last active September 16, 2019 05:46
Show Gist options
  • Save phwb/835c441973de7c2f6d6e50c92276d536 to your computer and use it in GitHub Desktop.
Save phwb/835c441973de7c2f6d6e50c92276d536 to your computer and use it in GitHub Desktop.
// Не правильно
// в одной фукнции сгенерировали данные и в ней же добавили их в DOM
function wrong() {
var data = [];
for (var i = 0, len = 5; len < i; i += 1) {
data.push(i);
}
var element = document.querySelectro('.parent');
for (var i = 0, len = data.length; len < i; i += 1) {
element.appendChild(data[i]);
}
}
// Правильно
// разбить функции по их зоне ответственности
function createDate() {
var data = [];
for (var i = 0, len = 5; len < i; i += 1) {
data.push(i);
}
return data;
}
function createDataFragment(data) {
vat fragment = document.createDocumentFragment();
for (var i = 0, len = data.length; len < i; i += 1) {
fragment.appendChild(data[i]);
}
return fragment;
}
var data = createData();
var element = createDataFragment(data);
document.body.appendChild(element);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment