Javascript:Creating&Managing Classes.Best Code Structure?
I have a simple class like:
function obj(id) {
this.id = id;
}
I have an array and save my obj instances in this array:
var objs = new Array();
And a counter(not so necessary but I use it).It counts the obj instances.
var objsc = -1;
To manage(add,del,get,create) instances I have a manager class prototype.
ext function of objman (obj manager class):Check if this obj with this id exists or not.
cre function:creates an instances and add to the Array.
get function:returns obj from Array
num function:returns the place of obj in the Array.
del function:deletes the obj from Array.
function objman() {
this.ext = function (id) {
for (i = 0; i <= objsc; i++) {
if (objs[i].id == id) {
return true;
}
}
return false;
}
this.cre = function (id) {
ext = this.ext(id);
if (ext == false) {
objsc += 1;
objs[objsc] = new obj(id);
}
}
this.get = function (id) {
for (i = 0; i <= objsc; i++) {
if (objs[i].id == id) {
return objs[i];
}
}
}
this.num = function (id) {
for (i = 0; i <= objsc; i++) {
if (objs[i].id == id) {
return i;
}
}
}
this.del = function (id) {
ext = this.ext(id);
num = this.num(id);
if (ext == true) {
objs[num] = objs[objsc];
objs.length = objsc;
objsc -= 1;
}
}
}
To use this objman I create an Instance of it.
var myobjman = new objman();
Ok. When body loaded;(<body onload="bol();"></body>):
bol function is:
function bol() {
myobjman.cre(‘id1′);
myobjman.cre(‘id2′);
myobjman.cre(‘id3′);
myobjman.cre(‘id4′);
myobjman.cre(‘id5′);
myobjman.cre(‘id6′);
myobjman.cre(‘id7′);
alert(myobjman.get(‘id3′).id);
myobjman.del(‘id3′);
}
This code WORKS. No problem.
My question is: Do you have any better idea, to create a custom object(class) and manage instances of the object(class)?
I mean do you have better code structure alternatives?
Hope I am clear..Thanks..
<script type="text/javascript">
var wins = new Array();
var mywinman = new winman();
function bol() {
mywinman.cre(‘id1′);
mywinman.cre(‘id2′);
mywinman.cre(‘id3′);
mywinman.cre(‘id4′);
mywinman.cre(‘id5′);
mywinman.cre(‘id6′);
mywinman.cre(‘id7′);
alert(mywinman.get(‘id3′).id);
mywinman.del(‘id3′);
}
function winman() {
this.ext = function (id) {
for (i = 0; i <= wins.length – 1; i++) {
if (wins[i].id == id) {
return true;
}
}
return false;
}
this.cre = function (id) {
ext = this.ext(id);
if (ext == false) {
wins[wins.length] = new win(id);
}
}
this.get = function (id) {
for (i = 0; i <=
this.get = function (id) {
for (i = 0; i <= wins.length – 1; i++) {
if (wins[i].id == id) {
return wins[i];
}
}
}
this.num = function (id) {
for (i = 0; i <= wins.length – 1; i++) {
if (wins[i].id == id) {
return i;
}
}
}
this.del = function (id) {
ext = this.ext(id);
num = this.num(id);
if (ext == true) {
wins[num] = wins[wins.length - 1];
wins.length = wins.length – 1;
}
}
}
function win(id) {
this.id = id;
}
</script>
January 15th, 2010 at 11:47 pm
You do a whole lot of searching there.
Writing a.b is the same as writing a["b"]. So you can create an object and store things in it by saying:
a["b"] = 42;
Equally well you can text if there is already a value there thus:
if (a["b"] == null) a["b"] = 10;
And where I’ve used the constant "b", you can use a variable.
function ext(id)
{
return a[id] != null;
}
And it should make your code smaller and faster!