PDA

View Full Version : Plz help me Evaluate my Code



???
06-18-2007, 06:24 PM
I made a thing (not actually a class) that is like a database. I just did it because i was bored, but could you give me suggestions on how to improve it? It's code is:

var Database = function (data) {
Database.prototype.database = new Array ();
Database.prototype.setData = function (name,value) {
Database.prototype.database[name] = value;
}
Database.prototype.getData = function (name) {
return Database.prototype.database[name];
}
Database.prototype.deleteData = function (name) {
Database.prototype.setData (name,null);
}
var dataSplit = data.split (";");
for (i=0; i < dataSplit.length; i ++) {
dataSplit[i] = dataSplit[i].split ("=");
Database.prototype.setData (dataSplit[i][0],dataSplit[i][1]);
}
}

You would use it with something like:

var myData = new Database ("loler=hi");
alert (myData.getData ("loler"));

Or:

var myData = new Database ();
myData.setData ("loler","hi");
alert (myData.getData ("loler"));

And it would create an alertbox saying hi, or whatever "loler" is.
Anything I did wrong? It works, but I still might have messed up.

Trinithis
06-18-2007, 07:55 PM
Don't put the prototypes inside the constructor function. If you want something to it's effect, use the "this" keyword.



function Database(data) {
var dataSplit = data.split (";");
for (i=0; i<dataSplit.length; ++i) {
dataSplit[i] = dataSplit[i].split ("=");
this.setData(dataSplit[i][0],dataSplit[i][1]);
}
}
Database.prototype.database = new Array();
Database.prototype.setData = function (name,value) {
Database.prototype.database[name] = value;
}
Database.prototype.getData = function(name) {
return Database.prototype.database[name];
}
Database.prototype.deleteData = function(name) {
Database.prototype.setData (name,null);
}

???
06-18-2007, 10:02 PM
Why shouldn't you put it in there? I've been told that before but I don't see why not. Thanks anyway.

Trinithis
06-18-2007, 10:06 PM
A prototype for an object in the sense of the word is literally like a prototype. If the object does not have a certain property, then it looks at its "prototypical" object and then sees if it has it. If it does, then it uses that property or method. So in general, many objects may have a method that does the same thing with every object of its type, which should be defined in the prototype. When a value varies from object to object, then the constructor function should define the property.

Twey
06-19-2007, 12:40 AM
Because if you do, you're creating an entirely new prototype object every time you create a new instance, which completely defeats the point. You only need one prototype object. You're essentially making a hashtable, too, so you might as well call it one and pass an object, not a string, as the initial data:
function Hash(/* Object */ data) {
if(data)
for(var x in data)
if(data.hasOwnProperty(x))
this[x] = data[x];
}The rest of your code is superfluous, as the functionality is provided natively. You could, however, add some more handy methods:
function zip(/* Array arr1, Array arr2, ... */) {
var i, j, r = [], v, iter = 0;
for (i = 0; i < arguments.length; ++i)
iter = Math.max(arguments[i].length, iter);
for (i = 0; i < iter; ++i) {
for (j = 0, v = []; j < arguments.length; ++j)
v.push(arguments[j][i]);
r.push(v);
}
return r;
}

Hash.prototype = {
'keys' : function() {
var r = [];
for(var x in this)
if(this.hasOwnProperty(x))
r.push(x);
return r;
},
'values' : function() {
var r = [];
for(var i = 0, k = this.keys(), n = k.length; i < n; ++i)
r.push(this[k[i]]);
return r;
},
'items' : function() {
return zip(this.keys(), this.values());
},
'size' : function() {
return this.keys().length;
},
'map' : function(/* function(key, value) */ f) {
var r = [];
for(var i = 0, k = this.keys(), n = k.length; i < n; ++i)
r.push(f(k[i], this[k[i]]));
return r;
},
'extend' : function(/* Hash */ newData) {
var r = new Hash(this);
for(var i = 0, k = newData.keys(), n = k.length; i < n; ++i)
r[k[i]] = newData[k[i]];
return r;
}
};

???
06-19-2007, 03:06 PM
Hehe, um, I'm not very good at understanding other people's script, but I did add a list of data names, and made a length get function (or method).