473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Variables and Object creation using prototypes

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

Apr 30 '06 #1
6 1139
On 30/04/2006 12:33, nr**@hotmail.com wrote:
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

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Apr 30 '06 #2
nr**@hotmail.com wrote:
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.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Apr 30 '06 #3
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 Winter wrote:
On 30/04/2006 12:33, nr**@hotmail.com wrote:
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

--
Michael Winter
Prefix subject with [News] before replying by e-mail.


Apr 30 '06 #4
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 Winter wrote:
On 30/04/2006 12:33, nr**@hotmail.com wrote:
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

--
Michael Winter
Prefix subject with [News] before replying by e-mail.


Apr 30 '06 #5
nr**@hotmail.com wrote:
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
--
Multiple exclamation marks are a sure sign of a diseased mind.
-- Terry Pratchett
Apr 30 '06 #6
Thomas 'PointedEars' Lahn said the following on 4/30/2006 5:04 PM:
nr**@hotmail.com wrote:
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?


Since when is it not? People ask questions, they may or may not get answers.
Get a life.


You first.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Tom Moroow | last post by:
Hi, I'm pretty new to javascript and was wondering how you would piece together a variable name and then assign it a value. I want to create a hidden field and assign it a value based on the value...
6
by: David T. Ashley | last post by:
Hi, In my project, I typically declare and define variables in the .H file, i.e. DECMOD_MAIN UINT8 can_message_201_status_global #ifdef MODULE_MAIN = HAS_NEVER_BEEN_RECEIVED #endif ;
7
by: Bennett Haselton | last post by:
Is there any way to find a string representing an object's class, which will work in Internet Explorer 6? "typeof" doesn't work -- it returns "object" for all objects: x =...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
10
by: Kleenex | last post by:
Reason: I am working on an embedded project which has very limited memory (under 512 bytes, 60 or so of which is stack space), which translates into limited stack space. In order to save on stack...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
4
by: Jim Carlock | last post by:
I've been using global to expose variables. However, I noticed the following... (1) global seems to use the nearest declaration. For example, // module1.php $sMyString = "Hello World.";
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.