Variables and Object creation using prototypes
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Variables and Object creation using prototypes

From: <nrlz@hotmail.com>
Date: Sun Apr 30 2006 - 13:33:43 CEST

Try to guess what happens if I run the following code in any modern
browser. Don't cheat! Guess before you try it out.
Will I get:

1) 1,2...a,b
2) a,b...a,b
3) null...null
4) 1,2,a,b...1,2,a,b

Here is the code:

function Group() {
}

Group.prototype = {
  members: [],

  addMember: function(m) {
    this.members.push(m);
  },

  listMembers: function() {
    return this.members.join(",");
  }
};

var my1 = new Group();
my1.addMember(1);
my1.addMember(2);

var my2 = new Group();
my2.addMember("a");
my2.addMember("b");

alert(my1.listMembers() + "..." + my2.listMembers());

Cheers!
Nathar
Received on Mon May 1 05:27:49 2006