let tab = [10, 11];
console.log(tab);
console.log('------- Boucle for in');
for (let index in tab){
console.log(` ${index} => ${tab[index]}`);
}
console.log('------- Boucle for in hasOwnProperty');
for (let index in tab){
if (tab.hasOwnProperty(index))
console.log(` ${index} => ${tab[index]}`);
}
console.log('------- Boucle for of ');
for (let val of tab){
console.log(` ${val} `);
}
console.log('------- Boucle forEach ');
tab.forEach(function (element, index,tab) {
console.log(` ${index} => ${element}`);
});
console.log('------- Boucle Object.keys');
Object.keys(tab).forEach(function (element, index) {
console.log(` ${index} => ${element}`);
});
Les différences entre toutes ces méthodes apparaitra plus clairement en modifiant les attributs.
Object.prototype.objCustom = function AjoutFxObj() {};
Array.prototype.arrCustom = function AjoutFxArr() {};
let tab = [10, 11];
tab[tab.length] = 12;
tab.myProp = "une propri";