Variables and Object creation using prototypes

N

nrlz

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
 
M

Michael Winter

Try to guess what happens if I run the following code in any modern
browser. [...]

A "modern browser" doesn't matter. Any correct ECMAScript implementation
will do.
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
[snip]

Group.prototype = {
members: [],

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

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

The latter (4). The members array is a property of the prototype object,
not objects created via the Group constructor function. As each
resulting object will /share/ the properties of the prototype, any
properties added to it will accumulate and be accessible to all instances.

[snip]

Mike
 
M

Matt Kruse

function Group() {
}

function Group() {
this.members = [];
}

This might cause it to behave more like you'd expect. This way, the
'members' variable belongs to each instantiated Group object, rather than to
the shared prototype.
 
N

nrlz

You answered (4) and you are correct! Well done!

Incidentally, this script does require a browser, because it uses the
"alert" function.

Cheer!
Nathar

Michael said:
Try to guess what happens if I run the following code in any modern
browser. [...]

A "modern browser" doesn't matter. Any correct ECMAScript implementation
will do.
[snip]

The latter (4). The members array is a property of the prototype object,
not objects created via the Group constructor function. As each
resulting object will /share/ the properties of the prototype, any
properties added to it will accumulate and be accessible to all instances.

[snip]

Mike
 
N

nrlz

You answered (4) and you are correct! Well done!

Incidentally, this script does require a browser, because it uses the
"alert" function.

Cheer!
Nathar

Michael said:
Try to guess what happens if I run the following code in any modern
browser. [...]

A "modern browser" doesn't matter. Any correct ECMAScript implementation
will do.
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
[snip]

Group.prototype = {
members: [],

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

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

The latter (4). The members array is a property of the prototype object,
not objects created via the Group constructor function. As each
resulting object will /share/ the properties of the prototype, any
properties added to it will accumulate and be accessible to all instances.

[snip]

Mike
 
T

Thomas 'PointedEars' Lahn

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

Since when is this a quiz show? Get a life.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top