Results 1 to 6 of 6

Thread: Plz help me Evaluate my Code

  1. #1
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Plz help me Evaluate my Code

    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.

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Don't put the prototypes inside the constructor function. If you want something to it's effect, use the "this" keyword.

    Code:
    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);
    	}

  3. #3
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    Why shouldn't you put it in there? I've been told that before but I don't see why not. Thanks anyway.

  4. #4
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    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.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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:
    Code:
    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:
    Code:
    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;
      }
    };
    Last edited by Twey; 12-17-2007 at 07:10 AM. Reason: WTH? How did that this vanish?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    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).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •