473,396 Members | 1,703 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.

Advanced javascript - can anyone explain this intricacy?

Hi - I'm new to javascript and I was reading the book Javascript
Professional Projects - there is a fragment that has me a little
perplexed, and I was wondering if anyone could explain why and how it
works.
How is it that setting a property to null alters another property?
For example setting
optionObject.options[optionObject.selectedIndex] = null;
seems to set optionObject.selectedIndex to -1 automagically.

Can you attach "hooks" in javascript that get called whenever a
property is modified?
Or does an eventhandler get triggered behind the scenes that updates
selectedIndex - and if so - how do i attach similar eventhandlers to
objects - or is this an intrinsic feature of javascript?

Here is the function. I have inserted some debug messages which
illustrate that as soon as the property is set to null -
fromSelect.selectedIndex gets updated.
function swapSelects( fromSelect, toSelect )
{
var toSelect_Length = toSelect.options.length;
while( fromSelect.selectedIndex -1 )
{
var index = fromSelect.selectedIndex;
alert( "1>" + fromSelect.selectedIndex );
toSelect.options[toSelect_Length] = new Option(
fromSelect.options[index].text );
toSelect.options[toSelect_Length].value =
fromSelect.options[index].value;
alert( "2>" + fromSelect.selectedIndex );

// note this is the line that triggers the update
fromSelect.options[index] = null;
alert( "3>" + fromSelect.selectedIndex );

toSelect_Length++;
}

thanks in advance.
Faisal Vali.

Sep 17 '06 #1
11 1779

Faisal Vali wrote:
Hi - I'm new to javascript and I was reading the book Javascript
Professional Projects - there is a fragment that has me a little
perplexed, and I was wondering if anyone could explain why and how it
works.
How is it that setting a property to null alters another property?
For example setting
optionObject.options[optionObject.selectedIndex] = null;
seems to set optionObject.selectedIndex to -1 automagically.
I've tried to recreate this, but I can not. Please see the code on this
page:

http://www.krubner.com/testJavascript.htm

Sep 17 '06 #2

Faisal Vali wrote:
Hi - I'm new to javascript and I was reading the book Javascript
Professional Projects - there is a fragment that has me a little
perplexed, and I was wondering if anyone could explain why and how it
works.
How is it that setting a property to null alters another property?
For example setting
optionObject.options[optionObject.selectedIndex] = null;
seems to set optionObject.selectedIndex to -1 automagically.
You'll note that in the experiment that I posted here:

http://www.krubner.com/testJavascript.htm

things work as you would expect: it is the value that becomes null, not
the index.

I'm not clear on how you concluded that "How is it that setting a
property to null alters another property?"

I don't see that happening.

Sep 17 '06 #3

Faisal Vali wrote:
Here is the function. I have inserted some debug messages which
illustrate that as soon as the property is set to null -
fromSelect.selectedIndex gets updated.
function swapSelects( fromSelect, toSelect )
{
var toSelect_Length = toSelect.options.length;
while( fromSelect.selectedIndex -1 )
{
var index = fromSelect.selectedIndex;
alert( "1>" + fromSelect.selectedIndex );
toSelect.options[toSelect_Length] = new Option(
fromSelect.options[index].text );
toSelect.options[toSelect_Length].value =
fromSelect.options[index].value;
alert( "2>" + fromSelect.selectedIndex );

// note this is the line that triggers the update
fromSelect.options[index] = null;
alert( "3>" + fromSelect.selectedIndex );

toSelect_Length++;
}
I'm trying to make this function work on this page:

http://www.krubner.com/testJavascript.htm

The code is dying on the line where you reference fromSelect.options.
It is hard for me to imagine what kind of parameters you're trying to
pass in to this function. The parameters seem to be complex objects
that are both arrays themselves and also contain arrays. Had you gone
into more detail, that would have been helpful. I tried to guess what
kind of objects you were using as parameters, but I've given when I
realized the complexity of these things.

For now I get this error:

Error: fromSelect.options has no properties
Please note, I also had to explicitly assign a value of "0" to
fromSelect.selectedIndex to get even as far as I did.

Sep 17 '06 #4
Faisal Vali wrote:
Hi - I'm new to javascript and I was reading the book
Javascript Professional Projects - there is a fragment
that has me a little perplexed, and I was wondering if
anyone could explain why and how it works.

How is it that setting a property to null alters another
property?
For example setting
optionObject.options[optionObject.selectedIndex] = null;
seems to set optionObject.selectedIndex to -1 automagically.
There was once a time when the browser's object model and JavaScript(tm)
where virtually the same thing. Standardisation has acted to separate
the two, and we now have a core language (specified by ECMA 262 3rd
Edition), vendor specific extensions to that language, and the object
model provided by the host of the scripts. This allows a standardised
javascript to be used for scripting more types of host than just web
browser (such as PhotoShop from version 7).

Assuming the above - optionObject - is a reference to the DOM
representation of an HTML SELECT element (which is necessary to explain
the phenomenon described) the behaviour of the object when it is
modified is not determined by javascript at all. It is what is
classified as a "host object", and so is not part of the language.

For the DOM representation of a SELECT element (without the MULTIPLT
attribute) the - selectedIndex - property is the index value of the
currently selected OPTION element. The act of assigning - null - to a
member of the SELECT element's - options - collection is to remove that
OPTION element from the SELECT element. And if you remove the currently
selected OPTION element it can no longer be the currently selected
OPTION element, so the - selectedIndex - property of the SELECT element
must be changed.

As an OPTION element should not become selected unless the user selects
it the - selected Index - property must be set to a value that is not an
index of an OPTION element, and one that can be used to signal to
scripts that no OPTION element is selected. The value of -1 is used for
that purpose.
Can you attach "hooks" in javascript that get called
whenever a property is modified?
<snip>

In the standardised core of javascript you cannot. As a result it is
extremely unlikely that cross-browser code would ever attempt to do so.
However, some implementations have a - watch - extension that could be
used in that way, and the JavaScript(tm) implementation has -
__defineGetter__ - and - __defineSetter__ - extension that can allow
code to react to property changes. JScritp in IE has none of these, but
some of the IE browser's host objects support an - onpropertychange -
event handler that can be triggered by some property changes.

For pure javascript objects it is possible to adopt a design pattern
that provides 'setter' functions and insists that they are exclusively
used to set the values of properties. This allows side-effect code to be
triggered in response to changes in those property values, and it is
possible to then implement event handlers on javascript objects and have
them triggered by the side-effect code in the 'setters'.

Richard.
Sep 17 '06 #5

Faisal Vali wrote:
Can you attach "hooks" in javascript that get called whenever a
property is modified?
Or does an eventhandler get triggered behind the scenes that updates
selectedIndex - and if so - how do i attach similar eventhandlers to
objects - or is this an intrinsic feature of javascript?
I don't believe there is an event handler for a change of properties,
though you could use javascript closures to enforce such a thing:

objectForSettingUserNumbers = function setNumberOfUsers(numberOfUsers)
{
var howManyUsersOnThisPage = 0;

var controllerForHowManyUsers = function(users) {
howManyUsersOnThisPage = users;
alert("howManyUsersOnThisPage now equals " +
howManyUsersOnThisPage);
}

controllerForHowManyUsers(numberOfUsers);
return controllerForHowManyUsers;
}

This sets howManyUsersOnThisPage to 8 :

objectForSettingUserNumbers(8);
The variable howManyUsersOnThisPage is now completely encapsulated,
like a private class variable in any object oriented language - there
is no way to reach it accept through the method we've allowed. Thus,
yes, here you could set triggers that go off whenever someone tries to
reach this value.

I put this code up on the same test page as before:

http://www.krubner.com/testJavascript.htm

Sep 17 '06 #6

Jake Barnes wrote:
Faisal Vali wrote:
Can you attach "hooks" in javascript that get called whenever a
property is modified?
Or does an eventhandler get triggered behind the scenes that updates
selectedIndex - and if so - how do i attach similar eventhandlers to
objects - or is this an intrinsic feature of javascript?

I don't believe there is an event handler for a change of properties,
though you could use javascript closures to enforce such a thing:

Actually, this would be more flexible than my first example:

function setNumberOfUsers()
{
var howManyUsersOnThisPage = 0;

var controllerForHowManyUsers = function(users) {
howManyUsersOnThisPage = users;
alert("howManyUsersOnThisPage now equals " +
howManyUsersOnThisPage);
}
return controllerForHowManyUsers;
}
objectForSettingUserNumbers = setNumberOfUsers();

Now objectForSettingUserNumbers is a reference to the anonymous
function stored in controllerForHowManyUsers. You can continue to
change the value of howManyUsersOnThisPage, but only using this one
method:

This sets howManyUsersOnThisPage to 8 :

objectForSettingUserNumbers(8);

or 18:

objectForSettingUserNumbers(18);
The variable howManyUsersOnThisPage is now completely encapsulated,
like a private class variable in any object oriented language - there
is no way to reach it accept through the method we've allowed. Thus,
yes, here you could set triggers that go off whenever someone tries to
reach this value.

I put this code up on the same test page as before:

http://www.krubner.com/testJavascript.htm
Sep 18 '06 #7

Faisal Vali wrote:
Hi - I'm new to javascript and I was reading the book Javascript
Professional Projects - there is a fragment that has me a little
perplexed, and I was wondering if anyone could explain why and how it
works.
How is it that setting a property to null alters another property?
For example setting
optionObject.options[optionObject.selectedIndex] = null;
seems to set optionObject.selectedIndex to -1 automagically.

Can you attach "hooks" in javascript that get called whenever a
property is modified?
Or does an eventhandler get triggered behind the scenes that updates
selectedIndex - and if so - how do i attach similar eventhandlers to
objects - or is this an intrinsic feature of javascript?

Here is the function. I have inserted some debug messages which
illustrate that as soon as the property is set to null -
fromSelect.selectedIndex gets updated.
function swapSelects( fromSelect, toSelect )
{
var toSelect_Length = toSelect.options.length;
while( fromSelect.selectedIndex -1 )
{
var index = fromSelect.selectedIndex;
alert( "1>" + fromSelect.selectedIndex );
toSelect.options[toSelect_Length] = new Option(
fromSelect.options[index].text );
toSelect.options[toSelect_Length].value =
fromSelect.options[index].value;
alert( "2>" + fromSelect.selectedIndex );

// note this is the line that triggers the update
fromSelect.options[index] = null;
alert( "3>" + fromSelect.selectedIndex );

toSelect_Length++;
}

thanks in advance.
Faisal Vali.
In javascript -1 equates to null, and null can equate to false.

What happens is you set the selected object to a null, so when you ask
it for the selected index, it returns you -1 (null which will also
equate to false).

Sep 18 '06 #8

Tom Cole wrote:
Faisal Vali wrote:
Hi - I'm new to javascript and I was reading the book Javascript
Professional Projects - there is a fragment that has me a little
perplexed, and I was wondering if anyone could explain why and how it
works.
How is it that setting a property to null alters another property?
For example setting
optionObject.options[optionObject.selectedIndex] = null;
seems to set optionObject.selectedIndex to -1 automagically.
[...]
>
In javascript -1 equates to null, and null can equate to false.

What happens is you set the selected object to a null, so when you ask
it for the selected index, it returns you -1 (null which will also
equate to false).
You should have read Richard's response. An HTML select element is not
a built-in JavaScript object, it is provided by the host environment.
In most browsers, its behaviour conforms to the W3C DOM HTML
specification which says that if no option is selected, the
selectedIndex property should return -1.

<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-85676760 >

In JavaScript, -1 is not equivalent to 'null'. When evaluated in a
conditional expression it is type converted to boolean true. According
to the ECMAScript Specification section 9.2, the numbers +0, -0 and NaN
type convert to false, any other number converts to true.

Try it:

alert( !!(-1) ); // Shows true
The fun part here is working out what should happen when the
selectedIndex property is set to null. According to the W3C
specification, the selectedIndex property is a long, which in
JavaScript equates to a number primitive. When converting 'null' to a
number using toNumber, null should be converted to +0.

It might therefore be argued that assigning null to the selectedIndex
property should result in it being set to +0, thereby making the first
option selected. Clearly that logic is flawed... :-)
--
Rob

Sep 18 '06 #9
RobG wrote:
Tom Cole wrote:
<snip>
>In javascript -1 equates to null, and null can equate to false.
<snip>
In JavaScript, -1 is not equivalent to 'null'. When evaluated in a
conditional expression it is type converted to boolean true.
<snip>
It might therefore be argued that assigning null to the selectedIndex
property should result in it being set to +0, thereby making the first
option selected. ...
Fortunately the posted code never did assign null to the -
selectedIndex - property so null's type-converting to numeric zero is
not significant.

Richard.

Sep 19 '06 #10
Dear Richard - thank you very much for your insightful and helpful
answer - I do have some comments/questions.

Richard Cornford wrote:
Faisal Vali wrote:
>><snip
How is it that setting a property to null alters another
property?
For example setting
optionObject.options[optionObject.selectedIndex] = null;
seems to set optionObject.selectedIndex to -1 automagically.

There was once a time when the browser's object model and JavaScript(tm)
where virtually the same thing. Standardisation has acted to separate
the two, and we now have a core language (specified by ECMA 262 3rd
Edition), vendor specific extensions to that language, and the object
model provided by the host of the scripts. This allows a standardised
javascript to be used for scripting more types of host than just web
browser (such as PhotoShop from version 7).
aah - ok that does explain the phenomenon that i found perplexing.
Assuming the above - optionObject - is a reference to the DOM
representation of an HTML SELECT element (which is necessary to explain
the phenomenon described) the behaviour of the object when it is
modified is not determined by javascript at all. It is what is
classified as a "host object", and so is not part of the language.
Your assumption/intuition is indeed correct and the arguments do refer
to an HTML SELECT element.
>
For the DOM representation of a SELECT element (without the MULTIPLT
attribute) the - selectedIndex - property is the index value of the
currently selected OPTION element. The act of assigning - null - to a
member of the SELECT element's - options - collection is to remove that
OPTION element from the SELECT element. And if you remove the currently
selected OPTION element it can no longer be the currently selected
OPTION element, so the - selectedIndex - property of the SELECT element
must be changed.
Aah - once again, thanks - i do have one question - what reference
clearly explains and documents the behaviors of the host objects?

thanks again in advance :-)
regards,
faisal vali.

<snip useful stuff about patterns for implementing property hooks and
more>

Sep 20 '06 #11
Faisal Vali wrote:
<snip>
i do have one question - what reference clearly explains
and documents the behaviors of the host objects?
The W3C DOM documentation describes what the specified parts of a
browser object model should do. The browser manufacturer's documentation
(when made available), more or less, explain what their host objects do.
Apart from that it is mostly research through google and trial and error
experimentation.

Richard.
Sep 20 '06 #12

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

Similar topics

5
by: Robert Peake | last post by:
Hi, I notice a strange behavior in PHP4 and wonder if anyone can explain it. Essentially, when I extend a class from a built-in module, I can add and access variables but can not access new...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
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?
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
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
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...
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,...

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.