I have the following code:
function Vanguard(name,grade,skill,power,shield,critical, type, nation, clan, race, imageURL){
this.name = name;
this.grade = grade;
this.skill = skill;
this.power = power;
this.shield = shield;
this.critical = critical;
this.type = type;
this.nation = nation;
this.clan = clan;
this.race = race;
this.imageURL = imageURL;
};
var database = {};
database['asura kaiser'] = new Vanguard("Asura Kaiser", 3, "Twin Drive!!", 11000, 0, 1, "Normal Unit", "Star Gate", "Nova Grappler", "Battleroid", "http://images4.wikia.nocookie.net/__cb20120428001646/cardfight/images/thumb/a/a6/VGE_BT01-008EN.jpg/300px-VGE_BT01-008EN.jpg");
database['king of knights, alfred'] = new Vanguard("King of Knights, Alfred", 3, "Twin Drive!!", 10000, 0, 1, "Normal Unit", "United Sanctuary", "Royal Paladin", "Human", "http://images2.wikia.nocookie.net/__cb20121009013434/cardfight/images/thumb/9/95/BT01-001EN_RRR.jpg/300px-BT01-001EN_RRR.jpg");
database['dragonic overlord'] = new Vanguard("Dragonic Overlord", 3, "Twin Drive!!", 11000, 0, 1, "Normal Unit", "Dragon Sanctuary", "Kagerou", "Dragon", "");
database['ceo amaterasu'] = new Vanguard("CEO Amaterasu", 3, "Twin Drive", 10000, 0, 1, "Normal Unit", "United Sanctuary", "Oracle Think Tank", "Human", "");
database['alfred - early'] = new Vanguard("Alfred - Early", 3, "Twin Drive!!", 10000, 0, 1, "Normal Unit", "United Sanctuary", "Royal Paladin", "Human", "");
function printVanguard(p, name){
for (var p in database[name]){
document.getElementById('output').innerHTML +=('<b>' + p.charAt(0).toUpperCase() + p.slice(1) + '</b>: ' + database[name][p] + '<br />');
}
};
In my printVanguard function, is there a way I can use the for var in loop but print out everything EXCEPT the variable imageURL? Right now the code works but I’m looking to modify it so that the imageURL variable in the Vanguard object does NOT print out.
(This is not my full code, only what’s relevant. If you need my full code let me know.)
Actually it’s not a variable, but a property. You can prevent outputting it by adding a simple condition:
If you are adding prototype methods to your
Vanguards, you also might consider usinghasOwnPropertyto omit inherited properties as well. However, for using this code in production you hopefully won’t enumerate just the whole object, but either explicitly pick properties or use a nested object which contains only the data.Or you can skip the property with the loop by making it non-enumerable. However, this is a ES 5 feature and will not work in older browsers: