Quite new to OO Js, used to program with function after function so trying to fix that now!
I’m making a tab layout –
I create a tab by calling: tab.NewTab();
I can access the tabs at tab[0], tab[1] etc
var tabCount = 0;
var tabs = [];
tabs.NewTab = function (){
var tabName = "tab" + tabCount;
tabs[tabCount] = new Tab(tabName);
tabCount++;
};
function Tab(tabName){
return{
name: tabName
}
}
I wanted to make a function that counts how many tabs are open:
tabs.HowMany = function () {
for (var i in tabs) {
alert("new");
}
};
This is returning the methods too (0,1, NewTab, HowMany).
Any advice?
You’re looking for
tabs.push(new Tab(tabName));Then ditch
tabCount, and instead use thelengthproperty native to all javascript arrays:tabs.lengthAlso, your Tab constructor is wrong. As it is currently written it should not be called with
new. Just callTab('someName')and it will return to you the object you’re looking for. If you do that however, change it totabsince non-constructor functions should be lowercase.If you’re really eager to use the
newkeyword, this is what Tab should look like:EDIT
If you want to iterate over all members of your array, this is the simplest way: