473,396 Members | 2,011 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,396 software developers and data experts.

Object referencing

Hey everyone,

I'm currently reading Sitepoint's 'Javascript Anthology' and keep
spotting a reference I'm not quite familiar with, such as
'document.getElementByTagName('p')[0]'. What I want to know is if this
reference points to the first element with the containing object?

Cheers

Mar 29 '07 #1
4 1540
On Mar 29, 11:21 am, "DoomedLung" <doomedl...@googlemail.comwrote:
Hey everyone,

I'm currently reading Sitepoint's 'Javascript Anthology' and keep
spotting a reference I'm not quite familiar with,
such as
'document.getElementByTagName('p')[0]'.
What is happening here is that you are creating an array, and
dereferencing the array, both on the same line.
document.getElementsByTagName('p') gets an array (actually its a
NodeList, but just think of it as an array).
And document.getElementsByTagName('p')[0] gets the first item from
that array.

//bar gets the first P tag in the document
var bar = document.getElementsByTagName('p')[0];

is shorthand for

//foo gets a list of all the P tags in the document
var foo = document.getElementsByTagName('p');
//bar gets the first P tag from that list
var bar = foo[0];
Mar 29 '07 #2
On Mar 29, 4:46 pm, "Noah Sussman" <blink...@gmail.comwrote:
On Mar 29, 11:21 am, "DoomedLung" <doomedl...@googlemail.comwrote:
Hey everyone,
I'm currently reading Sitepoint's 'Javascript Anthology' and keep
spotting a reference I'm not quite familiar with,
such as
'document.getElementByTagName('p')[0]'.

What is happening here is that you are creating an array, and
dereferencing the array, both on the same line.
document.getElementsByTagName('p') gets an array (actually its a
NodeList, but just think of it as an array).
And document.getElementsByTagName('p')[0] gets the first item from
that array.

//bar gets the first P tag in the document
var bar = document.getElementsByTagName('p')[0];

is shorthand for

//foo gets a list of all the P tags in the document
var foo = document.getElementsByTagName('p');
//bar gets the first P tag from that list
var bar = foo[0];
I thought as much. Just needed to clear it up.

Cheers dude :)

Mar 29 '07 #3
On Mar 29, 9:46 am, "Noah Sussman" <blink...@gmail.comwrote:
'document.getElementByTagName('p')[0]'.

What is happening here is that you are creating an array, and
dereferencing the array, both on the same line.
document.getElementsByTagName('p') gets an array

It's not array, it's a collection.

Mar 29 '07 #4
scripts.contact wrote:
On Mar 29, 9:46 am, Noah Sussman wrote:
>>'document.getElementByTagName('p')[0]'.

What is happening here is that you are creating an
array, and dereferencing the array, both on the same
line. document.getElementsByTagName('p') gets an array

It's not array, it's a collection.
The object returned from - getelementsByTagName - is a - NodeList
- not a collection. In a class-less language like ECMAScript being
a - NodeList - is just a matter of having the specified properties,
methods and characteristics of a - NodeList -, and as a result all
objects implementing the - HTMLCollection - interface are also
implementing the - NodeList - interface (so are NodeLists). But
NodeLists are not necessarily also HTMLCollections, and it would
be dangerous to assume that the objects returend from calls to
- getElementsById - have properties beyond those specified.

In class-based terminology it may be said that - HTMLCollection
- extends - NodeList, and actual examples may be implemented in
that way. In such a context it would be possible to case an -
HTMLCollection - into a - NodeList -, but not the other way around.

Richard.

Apr 1 '07 #5

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

Similar topics

2
by: Simon | last post by:
Am using the following code. <script language="JavaScript1.2"> function setquantity(productindex,productquantity) { //create the reference object irefname_none = eval("document." +...
4
by: Daniel Reber | last post by:
I have an exe that serializes one of it's classes to disk. I also have the exact same .cs file that contains the class that was serialized, in a Windows service application. I am trying to load...
10
by: Macka | last post by:
A few pieces of information first: * I have a class called Folder which represents a row of data in a database table. The data access side of things is not an issue. * The table has a parent...
6
by: Wilfried Mestdagh | last post by:
Hi, When I use same variable for a new created object, then whill the old one destroyed by garbage collection, or do I first have to set it to null ? eg: SomeObject o = new SomeObject();...
11
by: DogEye | last post by:
In the C# , I want to change the object only after the user click the "Submit" button , so I first new an object and use the "=" to get the object in memory , I found that the operation "=" only...
5
by: Christopher D. Wiederspan | last post by:
This is a bit tough to figure out the best way to ask, but here it goes. I've got a class, say MyObject, and everytime this class is instanciated, I actually want to get a reference to an existing...
9
by: DrConti | last post by:
Dear Python developer community, I'm quite new to Python, so perhaps my question is well known and the answer too. I need a variable alias ( what in other languages you would call "a pointer"...
6
by: woger151 | last post by:
If I write $x = new some_class($init_data1); and then $x = new some_class($init_data2); does the first object (constructed with $init_data1) get destroyed, or do I have to call unset() for...
14
by: Summercool | last post by:
The meaning of a = b in object oriented languages. ==================================================== I just want to confirm that in OOP, if a is an object, then b = a is only copying the...
32
by: Joe | last post by:
I am just starting to use Object Oriented PHP coding, and I am seeing quite often the following (this example taken from a wiki): $wakka =& new Wakka($wakkaConfig); What exactly is the =&, and...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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,...

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.