Doubt in Array , (beginner question)

S

sathyashrayan

Dear group,
pls go through the following function definition:

function at_show_aux(parent, child)
{
var p = document.getElementById(parent);
var c = document.getElementById(child);

var top = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
var left = (c["at_position"] == "x") ? p.offsetWidth +2 : 0;

for (; p; p = p.offsetParent)
{
top += p.offsetTop;
left += p.offsetLeft;
}

c.style.position = "absolute";
c.style.top = top +'px';
c.style.left = left+'px';
c.style.visibility = "visible";
}

The variable c is not declared as a Array object, but it was manipulate
as is it is a array.I searched the google for the key word "javascript
array", but it all explains only about the variable declared with Array()
object. Can any one point me to the correct source for a Beginner.Thanks.
 
E

Erwin Moller

sathyashrayan said:
Dear group,
pls go through the following function definition:

function at_show_aux(parent, child)
{
var p = document.getElementById(parent);
var c = document.getElementById(child);

var top = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
var left = (c["at_position"] == "x") ? p.offsetWidth +2 : 0;

for (; p; p = p.offsetParent)
{
top += p.offsetTop;
left += p.offsetLeft;
}

c.style.position = "absolute";
c.style.top = top +'px';
c.style.left = left+'px';
c.style.visibility = "visible";
}

The variable c is not declared as a Array object, but it was manipulate
as is it is a array.I searched the google for the key word "javascript
array", but it all explains only about the variable declared with Array()
object. Can any one point me to the correct source for a Beginner.Thanks.

Hi,

That notation is named associative Array notation (or something like that).
example:
var myObject = new Object();
myObject["firstname"] = "sathyashrayan";
myObject["favlang"] = "JavaScript";

That created a new object, and added 2 'hashes' to it with their values.
You can think of them as slots or arrayindexes that do not use a number, but
a string to point to a certain place.
This is sometimes refered to as properties of the Object.
You can also write:
myObject.firstname

They can hold other Objects, like array, or strings, or anything that you
can store in a variable.
They come in handy to create your own datastructures.

In your case you got your Object named c via getElementById().
Appearantly that c thing contains a hash/property named 'at_position', which
is not a standard property to my knowledge.

Regards,
Erwin Moller
 
R

RobG

sathyashrayan said:
Dear group,
pls go through the following function definition:

function at_show_aux(parent, child)
{
var p = document.getElementById(parent);
var c = document.getElementById(child);

var top = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
var left = (c["at_position"] == "x") ? p.offsetWidth +2 : 0;

for (; p; p = p.offsetParent)
{
top += p.offsetTop;
left += p.offsetLeft;
}

c.style.position = "absolute";
c.style.top = top +'px';
c.style.left = left+'px';
c.style.visibility = "visible";
}

The variable c is not declared as a Array object

It is declared and initialised in one statement, its value is set to
whatever is returned by document.getElementById(child), presumably a
DOM element.
but it was manipulate as is it is a array.

It's being treated as a reference to an object. It would appear from
subsequent code that the author of this script has elsewhere extended
the host DOM object to include an "at_position" property.

Such extension of DOM objects is not included in either ECMAScript or
W3C specifications, so it is browser dependent and likely to fail in at
least some browsers (if not most). A more widely supported method
would be to add the property in the source HTML and access it using the
element's getAttribute method:

<div id="divA" at_position="x"> ... </div>

<script type="text/javascript">
var d = document.getElementById('divA');
alert( d.getAttribute( 'at_position' ) );
</script>


You might like to read this thread:

<URL:
http://groups.google.com/group/comp...ate+extended+browser+objects#b5bf1f5cf9f6b69a
or search for the thread titled "Instantiate extended browser objects"
 
A

ASM

RobG a écrit :
A more widely supported method
would be to add the property in the source HTML

what would say W3C's validator about these "strange" attributes ?

wouldn't it be better to assign these attributes by JS too ?
 
M

Michael Winter

ASM said:
RobG a écrit :

what would say W3C's validator about these "strange" attributes ?

It would declare the document invalid, of course.
wouldn't it be better to assign these attributes by JS too ?

Why? That wouldn't really make the document any more valid. It would
simply hide it from the validator.

An alternate strategy would be to store the data within the script
itself. In the original code, the id attribute values of the relevant
elements are passed around, rather than object references. These strings
could be used as property names of some native object, rather than
adding properties to host objects.

[snip]

Mike
 
M

Michael Winter

Erwin said:
sathyashrayan wrote:
[snip]
var top = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
[snip]

That notation is named associative Array notation (or something like
that).

Bracket notation; there are no associative arrays in ECMAScript. It is
an expression form of dot notation, though it has the added benefit of
allowing the use of property names that do not conform to the Identifier
grammar production. The above could have been written as:

var top = (c.at_position == 'y') ? p.offsetHeight + 2 : 0;

and it would act in precisely the same way.
var myObject = new Object();
myObject["firstname"] = "sathyashrayan";
myObject["favlang"] = "JavaScript";

The same applies to the latter two lines, above.
That created a new object, and added 2 'hashes' to it with their
values.

Properties. Objects are a collection of unordered properties, nothing more.
You can think of them as slots or arrayindexes that do not use a
number, but a string to point to a certain place.

In my opinion, it would be better to think of them for what they are.
Adding levels of indirection only add the potential for confusion. An
array index, for example, has a very precise definition in this language
and arbitrary strings do not qualify.
This is sometimes refered to as properties of the Object.

They are rarely referred to as anything else, in my experience.
You can also write:
myObject.firstname

And probably should, if there's no reason not to do so.

[snip]

Mike
 
E

Erwin Moller

Michael Winter wrote:

Hi Michael,
Erwin said:
sathyashrayan wrote:
[snip]
var top = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
[snip]

That notation is named associative Array notation (or something like
that).

Bracket notation; there are no associative arrays in ECMAScript. It is
an expression form of dot notation, though it has the added benefit of
allowing the use of property names that do not conform to the Identifier
grammar production. The above could have been written as:

var top = (c.at_position == 'y') ? p.offsetHeight + 2 : 0;

and it would act in precisely the same way.

True.
So why the latter better than the former?

And don't be so sure about the naming of the beast.
O'Reilly 'Definitive guide' refers to them as Associative Arrays too.
(Chapter 8.6 fourth edition)
Many programminglanguage have them, and they are called 'hashes' or
'associative arrays'. Perl, PHP, Java, even VB has some implementation.

Besides that, I have no trouble with calling them bracket-notation.
:)
var myObject = new Object();
myObject["firstname"] = "sathyashrayan";
myObject["favlang"] = "JavaScript";

The same applies to the latter two lines, above.

Indeed they can be written in both ways.

I prefer the bracketnotation because that syntax resembles other languages,
PHP's namely.
I think this is just a matter of taste.
Both are OK.
Properties. Objects are a collection of unordered properties, nothing
more.

Properties, hashes, associative arrays, Michael, they all describe the same
concept.

And Objects are a lot more than unordered properties. Unless you stretch the
definition of 'property' to methods, constructors, etc, which is too much
IMHO.

In my opinion, it would be better to think of them for what they are.
Adding levels of indirection only add the potential for confusion. An
array index, for example, has a very precise definition in this language
and arbitrary strings do not qualify.

Why not?
Why do arbitrary strings not qualify as an index? I use them all the time
(succesfully) in all kind of languages...
They are rarely referred to as anything else, in my experience.

Well, that was before you met me. ;-)

And probably should, if there's no reason not to do so.

I can use the same argument as you did, only with switched positions.
Why write myObject.firstname if myObject["firstname"] is also fine?

Really, this is a matter of taste as far as I can judge.

I don't claim to be an expert on JavaScript, but in this situation I think
you are taking a position that is a little too strick for no valid reason.
I could be wrong of course.

Cheers.

Regards,
Erwin Moller
[snip]

Mike
 
R

RobG

ASM said:
RobG a écrit :

what would say W3C's validator about these "strange" attributes ?

It will complain, but for many people that is not an issue :). You
could create your own custom DTD and link to that instead of the W3C's,
but likely most browsers will ignore it, so it provides little, if any,
useful benefit.

wouldn't it be better to assign these attributes by JS too ?

Probably, but how you assign them becomes an architectural decision. I
just suggested one method, there are others, including:

- use a data object that holds attributes for elements of interest,
say keyed on ID
- put custom values in the class attribute.
 
G

Guy

sathyashrayan a écrit :
Dear group,
pls go through the following function definition:

function at_show_aux(parent, child)
{
var p = document.getElementById(parent);
var c = document.getElementById(child);

var top = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
var left = (c["at_position"] == "x") ? p.offsetWidth +2 : 0;

for (; p; p = p.offsetParent)
{
top += p.offsetTop;
left += p.offsetLeft;
}

c.style.position = "absolute";
c.style.top = top +'px';
c.style.left = left+'px';
c.style.visibility = "visible";
}

The variable c is not declared as a Array object, but it was manipulate
as is it is a array.I searched the google for the key word "javascript
array", but it all explains only about the variable declared with Array()
object. Can any one point me to the correct source for a Beginner.Thanks.

Bonjour,
c is not a Array but an object and c.style.position is a property of c.
G
 
R

Randy Webb

Erwin Moller said the following on 10/25/2006 8:21 AM:
Michael Winter wrote:

Hi Michael,
Erwin said:
sathyashrayan wrote: [snip]

var top = (c["at_position"] == "y") ? p.offsetHeight+2 : 0; [snip]

That notation is named associative Array notation (or something like
that).
Bracket notation; there are no associative arrays in ECMAScript. It is
an expression form of dot notation, though it has the added benefit of
allowing the use of property names that do not conform to the Identifier
grammar production. The above could have been written as:

var top = (c.at_position == 'y') ? p.offsetHeight + 2 : 0;

and it would act in precisely the same way.

True.
So why the latter better than the former?

It's not. Bracket Notation allows characters that Dot Notation doesn't
allow.
And don't be so sure about the naming of the beast.
O'Reilly 'Definitive guide' refers to them as Associative Arrays too.
(Chapter 8.6 fourth edition)

Then O'Reilly is dead wrong. JS doesn't have an Associative Array.
Many programminglanguage have them, and they are called 'hashes' or
'associative arrays'. Perl, PHP, Java, even VB has some implementation.

Just because "Many programming languages" have them doesn't mean JS has
them. "Many programming languages" also have a pre-distribution
compiler, where do I get one for JS?
 
M

Matt Kruse

Randy said:
Then O'Reilly is dead wrong. JS doesn't have an Associative Array.

Don't be confused, though.

A plain old Object in javascript can and does work as an "associative array"
in the way that most people would expect. With some restrictions and quirks
which may not even matter to most developers. The term "associative array"
doesn't have a formal definition that can be agred upon by everyone and all
languages, so a person's preconceived notions about what an "associative
array" is may or may not match up with how javascript's Object works.

I think this thread really sums it up: (or beats a dead horse, depending on
how you look at it)
http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/fb682ee563e1869f
 
M

Michael Winter

Erwin Moller wrote:

[Bracket notation vs. dot notation]
So why the latter better than the former?

I never said that one was better than the other. They both provide the
same function - accessing properties - and where the property name is a
legal Identifier, they are equivalent. However, the former does have a
specific use: permitting the use of otherwise illegal property names,
and generating property names from expressions.
And don't be so sure about the naming of the beast.

The specification uses the name "bracket notation", the FAQ for this
group uses that, and so do other regular posters in this group. The
simple fact that ECMAScript doesn't feature associative arrays makes
bracket notation a far better choice, in any case.
O'Reilly 'Definitive guide' refers to them as Associative Arrays too.

That doesn't mean very much. Most books on the subject are considered
poor, and Flanagan's "The Definitive Guide" is judged the least worst
(but that's hardly a resounding endorsement).
Many programminglanguage have them, and they are called 'hashes' or
'associative arrays'.

And all programming languages are different. Just because one (or even
many) has a particular feature doesn't mean that every other language
needs to have it, too.

A "safe" map can be implemented in ECMAScript, just as it can be in many
other languages. The point here is that the language doesn't have an
intrinsic object that displays the same characteristics and lives up to
the same expectations.

[snip]
Properties, hashes, associative arrays, Michael, they all describe
the same concept.

The latter bring with them assumptions about how such a feature
operates. This is clear from the confusion surrounding the issue which
results in posts to this group. In understanding it properly, one can
use an object /like/ an associative array, but that's not the same thing
as declaring objects to /be/ associative arrays.
And Objects are a lot more than unordered properties. Unless you
stretch the definition of 'property' to methods

A method is a property: it has a reference to a function object as its
value.
constructors ...

A constructor is just a function object.

[snip]
Why not? Why do arbitrary strings not qualify as an index?

Only certain property names affect array objects, or can be affected by
Array.prototype methods. Any other property is only a property,
disqualifying it as an array index.
I use them all the time (succesfully) in all kind of languages...

Again, other languages aren't this language.

[snip]
And probably should, if there's no reason not to do so.

I can use the same argument as you did, only with switched positions.
Why write myObject.firstname if myObject["firstname"] is also fine?

Doesn't the very reason for this thread suggest a problem in overusing
bracket notation? Learning from a decent source helps, of course, but at
least in using bracket notation with purpose rather than "just because",
its use might be understood for what it really is instead of some
magical array-oriented feature.

[snip]

Mike
 
E

Erwin Moller

Michael said:
Erwin Moller wrote:

[Bracket notation vs. dot notation]
So why the latter better than the former?

I never said that one was better than the other. They both provide the
same function - accessing properties - and where the property name is a
legal Identifier, they are equivalent. However, the former does have a
specific use: permitting the use of otherwise illegal property names,
and generating property names from expressions.
And don't be so sure about the naming of the beast.

The specification uses the name "bracket notation", the FAQ for this
group uses that, and so do other regular posters in this group. The
simple fact that ECMAScript doesn't feature associative arrays makes
bracket notation a far better choice, in any case.
O'Reilly 'Definitive guide' refers to them as Associative Arrays too.

That doesn't mean very much. Most books on the subject are considered
poor, and Flanagan's "The Definitive Guide" is judged the least worst
(but that's hardly a resounding endorsement).
Many programminglanguage have them, and they are called 'hashes' or
'associative arrays'.

And all programming languages are different. Just because one (or even
many) has a particular feature doesn't mean that every other language
needs to have it, too.

A "safe" map can be implemented in ECMAScript, just as it can be in many
other languages. The point here is that the language doesn't have an
intrinsic object that displays the same characteristics and lives up to
the same expectations.

[snip]
Properties, hashes, associative arrays, Michael, they all describe
the same concept.

The latter bring with them assumptions about how such a feature
operates. This is clear from the confusion surrounding the issue which
results in posts to this group. In understanding it properly, one can
use an object /like/ an associative array, but that's not the same thing
as declaring objects to /be/ associative arrays.
And Objects are a lot more than unordered properties. Unless you
stretch the definition of 'property' to methods

A method is a property: it has a reference to a function object as its
value.
constructors ...

A constructor is just a function object.

[snip]
Why not? Why do arbitrary strings not qualify as an index?

Only certain property names affect array objects, or can be affected by
Array.prototype methods. Any other property is only a property,
disqualifying it as an array index.
I use them all the time (succesfully) in all kind of languages...

Again, other languages aren't this language.

[snip]
You can also write:
myObject.firstname
And probably should, if there's no reason not to do so.

I can use the same argument as you did, only with switched positions.
Why write myObject.firstname if myObject["firstname"] is also fine?

Doesn't the very reason for this thread suggest a problem in overusing
bracket notation? Learning from a decent source helps, of course, but at
least in using bracket notation with purpose rather than "just because",
its use might be understood for what it really is instead of some
magical array-oriented feature.

[snip]

Mike

Hi Mike,

Thanks for your good answer.
I understand your points now.
I'll try to refer to them as properties and bracketnotation from now on to
reduce confusion.

Regards,
Erwin Moller
 
V

VK

Erwin said:
I'll try to refer to them as properties and bracketnotation from now on to
reduce confusion.

That should be a clear distinction made between something one can
emulate by language tools and something represented as a separate
programming entity. Say JavaScript doesn't have private or protected
scope modifiers but they can be close emulated in CC scope management
or by alternative means.

In this aspect ECMAScript specifications do not define associative
array (aka Hash, Map, Dictionary and N more legacy names).

JavaScript does have associative arrays in some implementations.
JScript has for a long time Enumerator wrapper and Dictionary
constructor. JavaScript 1.7 (coming with future release of Firefox 2.0)
makes the first important step in this direction by introducing
Iterator constructor.

Because any object by itself is a set of property:value pairs, one can
use an Object instance as a primitive form of associative array. Such
usage is natural and well accepted. One may call it "associative array"
which is all fine as long as the understanding remains that it is not a
usage of some special programming entity but the regular object
property:value notation.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top