[Javascript] Javascript and FF
Paul Novitski
paul at juniperwebcraft.com
Sun Aug 30 05:10:36 CDT 2009
At 8/29/2009 08:17 AM, Del Wegener wrote:
>I have a Javascript calculator
>http://www.edi-cp.com/products_a_series_at_estimator.php
>Which works as intended in IE and I believe it worked in earlier versions of
>FF. However nothing happens when I use it in FF Version 3.5.2 nor with
>Version 3.0.11
The following might not be relevant to your problems with Firefox 3.5.2, but:
I tried running your script through JSLint, Douglas Crockford's
JavaScript validator <http://www.jslint.com/>. Turns out JSLint
doesn't like 'with' statements, so I thought I'd comment them out in
your script to see if there were any problems beyond them. I didn't
have time to go through the whole series of warnings & errors for
your code, but I did notice that your 'with' statements are wonky, e.g.:
with (Math)
{
var k = Math.pow(10,place);
return (Math.round(numb*k))/k;
}
This code suggests the existence of Math.Math.pow() and
Math.Math.round(). The whole idea behind 'with' is that you name the
object up front and omit it in the block, e.g.:
with (Math)
{
var k = pow(10,place);
return (round(numb*k))/k;
}
cf.
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Object_Manipulation_Statements#with_Statement
As you probably know, 'with' is not particularly efficient. It has to
check all of the variables & methods in the block to see if they're
attached to the object in question. There's no shortcut to doing this
even with known core objects such as Math: since JavaScript allows us
to modify objects and their prototypes on the fly, the interpreter
can't predict which methods and properties an object will or will not
have from one statement to the next. This suggests that we not use
'with' in loops that iterate many times. (Your script doesn't do
this, Del; I'm just talking generally.) It also arguably makes code
harder to read by us humans without necessarily making the script
significantly more concise for us nor efficient for the computer. But
it's certainly a syntactically legal part of the language and in my
opinion its presence in your script shouldn't trigger validator errors.
The fact that you're mis-using 'with' isn't going to abort your
script in Firefox (I just tested one of your with blocks and it ran
fine), it will just eat up a few buckets of machine cycles while
JavaScript searches the Math object in vain for nonexistent methods.
Not a big deal. But it is one sign that your code needs cleaning up,
and that would be one place to start.
JSLint, by the way, is a very strict validator (as Douglas
emphasizes, "Warning! JSLint will hurt your feelings.") and you need
to work with it (for example, declare external globals in special
comments) to get it to approve valid scripting. And it disapproves of
some bits of scripting style that aren't actual dysfunctional, such
as using 'with' and calling functions before they're declared in the
source. But it's a powerful tool that can really help, especially
with large projects or those niggly little bugs that we can't find
with our own eyeballs.
Regards,
Paul
__________________________
Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com
More information about the Javascript
mailing list