473,651 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Naming problem on array object

I have a array to store some student information, eg
var s = new Array('2006001' , 'Apple Joker', '5B');

normally, We get the data using s[0], s[1],s[2] ...
How can I define them more readable like
s['studentID'], s['studentName'], s['class'] ?

Thank you

Jul 3 '06 #1
14 1698
Cylix wrote:
I have a array to store some student information, eg
var s = new Array('2006001' , 'Apple Joker', '5B');

normally, We get the data using s[0], s[1],s[2] ...
How can I define them more readable like
s['studentID'], s['studentName'], s['class'] ?
Make an Object of it instead of an array.
Objects behave like / are hashes.
eg:

var myClass = new Object();
myClass["studentID"] = 123;
myClass["studentNam e"] = "Cyclix";
etc.

Regards,
Erwin Moller
>
Thank you
Jul 3 '06 #2
>Erwin Moller wrote:
var myClass = new Object();
myClass["studentID"] = 123;
myClass["studentNam e"] = "Cyclix";
Thank you for your suggestion first,
Under this way,
Can I still using myClass[0], myClass[1] to get the value?

Jul 3 '06 #3
Cylix wrote:
>Erwin Moller wrote:
>var myClass = new Object();
myClass["studentID"] = 123;
myClass["studentNam e"] = "Cyclix";

Thank you for your suggestion first,
Under this way,
Can I still using myClass[0], myClass[1] to get the value?
No.

Object properties can only have one name, which is a string. You can
call them '0', '1', '2', etc. but then you might as well use an Array.

You should probably check the FAQ on how to access properties using
square brackets and dot notation:

<URL:http://www.jibbering.c om/faq/#FAQ4_39>
--
Rob
Jul 3 '06 #4
Hi,

RobG wrote:
Cylix wrote:
>>Erwin Moller wrote:

>>var myClass = new Object();
myClass["studentID"] = 123;
myClass["studentNam e"] = "Cyclix";


Thank you for your suggestion first,
Under this way,
Can I still using myClass[0], myClass[1] to get the value?


No.

Object properties can only have one name, which is a string. You can
call them '0', '1', '2', etc. but then you might as well use an Array.

You should probably check the FAQ on how to access properties using
square brackets and dot notation:

<URL:http://www.jibbering.c om/faq/#FAQ4_39>
If you have to, you could do this:

var myArray = new Array();
myArray[ 0 ] = myArray[ "myLabel1" ] = anObject;

This way the property is referenced by index and by label. However, I
would avoid that if I were you, it introduces some level of confusion as
to what the container really does.

Not even mentioning that you must always remember to delete both
references, or else the referenced object won't be garbage collected.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Jul 3 '06 #5
Laurent Bugnion wrote:
Hi,

RobG wrote:
>Cylix wrote:
[...]
>>Can I still using myClass[0], myClass[1] to get the value?
>No.
[...]
If you have to, you could do this:

var myArray = new Array();
myArray[ 0 ] = myArray[ "myLabel1" ] = anObject;

This way the property is referenced by index and by label. However, I
would avoid that if I were you, it introduces some level of confusion as
to what the container really does.

Not even mentioning that you must always remember to delete both
references, or else the referenced object won't be garbage collected.
Not at all - JavaScript isn't C++ :-)

Are you aware of any browser that won't run garbage collection once a UA
navigates away from a page[1]? If it's necessary at all, then:

myArray = null;

should to the trick, provided myArray was the only reference to the
object until recently referenced by myArray and other variables don't
hold any references to whatever it formerly referenced.

1. Other than the IE memory leak noted in the FAQ, which only occurs in
a specific circumstance that shouldn't be an issue here.

--
Rob
Jul 3 '06 #6
Hi,

RobG wrote:
Laurent Bugnion wrote:
>Hi,

RobG wrote:
>>Cylix wrote:

[...]
>>>Can I still using myClass[0], myClass[1] to get the value?

>>No.

[...]
>If you have to, you could do this:

var myArray = new Array();
myArray[ 0 ] = myArray[ "myLabel1" ] = anObject;

This way the property is referenced by index and by label. However, I
would avoid that if I were you, it introduces some level of confusion
as to what the container really does.

Not even mentioning that you must always remember to delete both
references, or else the referenced object won't be garbage collected.


Not at all - JavaScript isn't C++ :-)

Are you aware of any browser that won't run garbage collection once a UA
navigates away from a page[1]? If it's necessary at all, then:

myArray = null;

should to the trick, provided myArray was the only reference to the
object until recently referenced by myArray and other variables don't
hold any references to whatever it formerly referenced.

1. Other than the IE memory leak noted in the FAQ, which only occurs in
a specific circumstance that shouldn't be an issue here.
In the last web application I worked on (a building automation
management station), we have situations where a page isn't posted back
for 2 weeks at least, all the values on the page being refreshed every
30 seconds using AJAX. So believe me, memory is critical in such
situation. The whole "The page is going to be refreshed anyway" attitude
was valid 3 to 5 years ago, it's not anymore!

JavaScript is not C++... it's much more flexible and it's much easier to
make mistakes and to create huge memory leaks, believe me (been there
done that). It's even more important in JavaScript than in C++ (or at
least than in C#) to program cleanly and to avoid confusing scenarios.
The one where an array contains both indexed values and properties seems
confusing enough to me to recommend against it.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Jul 3 '06 #7
"Cylix" <cy*******@gmai l.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
I have a array to store some student information, eg
var s = new Array('2006001' , 'Apple Joker', '5B');

normally, We get the data using s[0], s[1],s[2] ...
How can I define them more readable like
s['studentID'], s['studentName'], s['class'] ?
var studentID = 0
var studentName = 1
var class = 2

Thus, s[0] = s[studentID].

Except that "class" is a reserved word.
Jul 3 '06 #8

Laurent Bugnion wrote:
Hi,

RobG wrote:
Laurent Bugnion wrote:
[...]
Not even mentioning that you must always remember to delete both
references, or else the referenced object won't be garbage collected.

Not at all - JavaScript isn't C++ :-)

Are you aware of any browser that won't run garbage collection once a UA
navigates away from a page[1]? If it's necessary at all, then:

myArray = null;

should to the trick, provided myArray was the only reference to the
object until recently referenced by myArray and other variables don't
hold any references to whatever it formerly referenced.

1. Other than the IE memory leak noted in the FAQ, which only occurs in
a specific circumstance that shouldn't be an issue here.

In the last web application I worked on (a building automation
management station), we have situations where a page isn't posted back
for 2 weeks at least, all the values on the page being refreshed every
30 seconds using AJAX. So believe me, memory is critical in such
situation. The whole "The page is going to be refreshed anyway" attitude
was valid 3 to 5 years ago, it's not anymore!
That is something of an extreme case, I'd expect that provided you
aren't constantly creating new objects and variables, it shouldn't be
an issue. I'm currently working on an intranet data entry, reporting
and workflow application with pages that will persist for some time,
hopefully not 2 weeks at a time!

JavaScript is not C++... it's much more flexible and it's much easier to
make mistakes and to create huge memory leaks, believe me (been there
done that). It's even more important in JavaScript than in C++ (or at
least than in C#) to program cleanly and to avoid confusing scenarios.
Which is better/worse is moot, but I take your point that memory
management should always be considered and if necessary, addressed.

Memory leaks in JavaScript *should* only be a product of a faulty
environment. If a script simply keeps adding more and more objects or
variables, it is plain poor coding that will cause problems
(eventually) in any language.

The one where an array contains both indexed values and properties seems
confusing enough to me to recommend against it.
Absolutely. The first question is what is the benefit of the same
object having two properties synchronised to the same value? And is
whatever benefit is gained worth the overhead of managing the object's
properties manually?

I expect that discussion to be specific to a particular circumstance -
such as a page that is updated at 30 second intervals (via AJAX?) and
not refreshed for 2 weeks. :-)
--
Rob

Jul 3 '06 #9
Actually, I just want to declaire something like the javascript style.
For example, we may reference a frame as
window.frames[0] or window.frames['ABC']

so, does it mean the design of javascript is poor in this case?

Jul 4 '06 #10

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

Similar topics

4
7144
by: Mark Broadbent | last post by:
stupid question time again to most of you experts but this is something that continually bothers me. I am trying to get into the habit of naming variables and controls in an assembly as per convensions. The thing is that Ive never really get the full reference to check against. Ive seen a couple of articles, but there always seems to be a bit missing. I also always seem to run into conflicting convensions both in code samples themselves...
3
4341
by: clintonG | last post by:
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET...
14
3128
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them... (both are "Pascal Case" with no leading or trailing qualifiers). For example... I'll be modelling something, e.g. a computer, and I'll
6
4062
by: dm1608 | last post by:
I'm relatively new to ASP.NET 2.0 and am struggling with trying to find the best naming convention for the BAL and DAL objects within my database. Does anyone have any recommendations or best practices for naming my objects? I currently have all my type classses simply called "JobSummaryClass" or "JobDetailsClass". These classes simply contain the public properties and the get/set functions for the object. Is this an appropriate naming...
61
3737
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
3
3402
by: rsine | last post by:
I have searched around a little and have yet to find a naming convention for the string builder object. I really do not want to use "str" since this is for string objects and thus could be confusing. Also, I would like a good source on the naming conventions to be used for other objects. It seems there are tons of sights with naming conventions but none seem to contain a complete list for all the objects in .Net. Is there such a...
114
7821
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for any comments. --
9
7022
by: BillCo | last post by:
I'm coming from a MS Access background and so I'm very used to and comfortable with the hungarian (Leszynski et al) naming conventions. However, I'm getting started into my first SQL Server Database and really want to use the appropriate up to date standard naming convention (ISO compliant). I think I have the general idea from raking though countless conflicting sites and posts, but I'm a bit stuck on what to do regarding pk / fk naming
5
1560
by: Pukeko | last post by:
Hi, this is an array that is used for a dropdown menu. var imageArray = new Array( "ecwp://" + document.location.host + "/massey/images/massey/ massey_07_fullres.ecw", "ecwp://" + document.location.host + "/sampleiws/images/usa/ 1metercalif.ecw", "ecwp://" + document.location.host + "/sampleiws/images/australia/ parramatta.ecw";
0
8352
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7297
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.