B
BloodSteam
Guest
Hallo,
Ich hab eine Funktion auf Stackoverflow gesehen jedoch weiß Ich nicht genau wie es funktioniert.
javascript - Advantages of createElement over innerHTML? - Stack Overflow
Ich hab eine Funktion auf Stackoverflow gesehen jedoch weiß Ich nicht genau wie es funktioniert.
javascript - Advantages of createElement over innerHTML? - Stack Overflow
Code:
function isArray(a){
// Wieso braucht man hier ein Object.prototype ? <-------------------------------------
return Object.prototype.toString.call(a) === "[object Array]";
}
function make(desc){
if( !isArray(desc) ){
// Wieso braucht man hier ein Object.prototype ? <-------------------------------------
return make.call(this, Array.prototype.slice.call(arguments));
}
let name = desc[0];
let attributes = desc[1];
let el = document.createElement(name);
let start = 1;
if( typeof attributes === "object" && attributes !== null && !isArray(attributes) ){
for( let attr in attributes ){
el[attr] = attributes[attr];
}
start = 2;
}
for( let i = start; i < desc.length; i++ ){
if( isArray(desc[i]) ){
el.appendChild( make(desc[i]) );
}else{
el.appendChild(document.createTextNode( desc[i] ));
}
}
return el;
}