Test for null or undefined?

T

Thomas Allen

Yeah, you should have thought of that before you blew in here and
insulted me (perhaps you forgot about that.)  I see from another
thread that you now want my advice on "Javascript libraries."  Don't
hold your breath.

It's not your advice I'm looking for, but a cogent opinion. It's funny
that you tell me to "search for it" when you're happy to repeat your
jQuery.fn.attr complaint dozens of times. It's funnier that somebody
who frequently harkens back to others' past posts would call another a
"snitch" for doing the same.

And let the record show that I did not insult _you_ but accused you of
committing "douchebaggery" which I maintain you were. Person !=
Actions...

Thomas
 
D

David Mark

It's not your advice I'm looking for, but a cogent opinion.

I don't think you'd know one if you saw one.
It's funny
that you tell me to "search for it" when you're happy to repeat your
jQuery.fn.attr complaint dozens of times. It's funnier that somebody

People keep asking. :)
who frequently harkens back to others' past posts would call another a
"snitch" for doing the same.

Rubbish, I virtually *never* refer to the past postings of others,
though citing postings (or other articles) to prove a point is hardly
tantamount to snitching.
And let the record show that I did not insult _you_ but accused you of
committing "douchebaggery" which I maintain you were. Person !=
Actions...

Yes, I know. It is your favorite word. Means anything that makes you
feel less confident in your abilities.

You should realize that houses of cards tend to collapse very suddenly
and completely. As a jQuery "programmer", I see a homeless shelter in
your future.
 
T

Thomas Allen

I don't think you'd know one if you saw one.


People keep asking.  :)


Rubbish, I virtually *never* refer to the past postings of others,
though citing postings (or other articles) to prove a point is hardly
tantamount to snitching.




Yes, I know.  It is your favorite word.  Means anything that makes you
feel less confident in your abilities.

You should realize that houses of cards tend to collapse very suddenly
and completely.  As a jQuery "programmer", I see a homeless shelter in
your future.

No, JavaScript is not even remotely my specialty, which is why I come
here asking for help. It's also why I rely on a JavaScript library (I
arrived at jQuery after trying MooTools). I'm mainly a Python/PHP dev
and I've never had trouble finding work.

But I do know my way around the language well enough to help with
easier questions, which is why I'll still be around here helping
people out with those (so that they can get some help without the BS
and condescension).

Thomas
 
D

David Mark

No, JavaScript is not even remotely my specialty,

That much is apparent. Do you know how to get and set properties? If
so, you are ahead of all of the other jQuery developers.
which is why I come
here asking for help.

You should tread lightly then (and sure as hell not on me!)
It's also why I rely on a JavaScript library (I
arrived at jQuery after trying MooTools). I'm mainly a

Of course. But you have no way to judge said library or the
alternatives. You are throwing darts at a map, hoping to hit Shangri-
la.
Python/PHP dev
and I've never had trouble finding work.

Who asked you? I sure as hell wouldn't hire a puerile pissant like
yourself, but there is no accounting for taste.
But I do know my way around the language well enough to help with
easier questions

No, you don't. Do you need citations?
which is why I'll still be around here helping
people out with those (so that they can get some help without the BS
and condescension).

Good luck with that!
 
T

Thomas 'PointedEars' Lahn

Garrett said:
I would avoid that, too, for the same reason. Using |arguments|
prevents
an implementation from making an optimization.

| 10.1.8 Arguments Object
|
| When control enters an execution context for
| function code, an arguments object is created
| as follows: [...]

Did you get that now? There can be no real optimization here unless the
implementation would forego standards compliance. And I (and others)
have already demonstrated that because of the dynamic nature of the
language it is virtually impossible for an implementation to determine
use or non-use of the ‘arguments' object before execution.
An ECMAScript implementation guarantees behavior.

Nonsense. The Specification could not be clearer in that regard, and
implementors are well-advised not to try any "optimizations" here.

(So much for debunking
an "unfounded assuption"). If a function body does not make a
reference
to |arguments| or |eval|, an implementation could create an optimized
call for that function.

The resources that would need to be invested to determine before
execution if there really is an reference to the arguments object are
better spent elsewhere.
Implementations may make a static analysis of the function to
determine
if an optimized call can be performed and perform such optimized call,

if the side effects of such call would not be observable
Nonsense.

It has been demonstrated that implementations actually seem to do
this.

Then their implementors are at fault, and the change should be reverted,
better sooner than too late.
However, it the question is to determine if a function foo:-

function foo(x){
alert(x === undefined);
}

- has been passed an argument, as in:-

foo(undefined)

- as opposed to -

foo()

- would be to check the arguments object.

10.1.3 guarantees that missing parameters get value |undefined|.

No, it doesn't:

undefined = "foo";

We've been over this. There is one bullet-proof way to determine if an
argument has been passed or not: checking arguments.length.
| If the caller supplies fewer parameter values than there are formal
| parameters, the extra formal parameters have value undefined.

undefined != ‘undefined’

or, if you prefer the less ambiguous version:

typeof undefined != "undefined"

This has been discussed on here in detail before.

But apparently you haven't been paying attention then either.


PointedEars
 
T

Thomas Allen

That is a redirect farm you loopy lunatic.




Exactly.  That URI is not advertised for a reason.




You have no idea.

Your site is so well unadvertised that it's invisible to Google. Still
in "stealth mode" after six years? What a joke. And for all your
insults, I expected work more impressive than this:

http://www.cinsoft.net/hartkelaw/index.html

I mean, couldn't you sell the guy a domain?
 
D

David Mark

Your site is so well unadvertised that it's invisible to Google. Still

Hello? McFly? It's not a site. It's a domain and it's none of your
business anyway (that's why it isn't advertised.)
in "stealth mode" after six years? What a joke. And for all your
insults, I expected work more impressive than this:

Hmmm. Type "browser scripting library" into Google (or whatever) and
see what you come up with. That is the only site on the domain and
Google sure seems to know about it. :)
http://www.cinsoft.net/hartkelaw/index.html

I mean, couldn't you sell the guy a domain?

Couldn't you go intercourse your mother?

http://www.hartkelaw.net/hartkelaw/index.html

Again, none of your business at all (unless you need a good lawyer.)
 
T

Thomas Allen

Couldn't you go intercourse your mother?

Eloquent indeed. How about you go see yours? She's right upstairs, I'm
sure. Now you see how it feels: You tell me that I'm unhirable, and
I'll _show_ that you are (not that anybody would want your attitude on
their team). I mean, if that's your work experience going back to
2002, that's pretty sad:
http://www.google.com/search?hl=en&...hts=&as_occt=any&cr=&as_nlo=&as_nhi=&safe=off

Anyway, enough time wasting. I have to wrap up changes on a website
since the client's board will be meeting tomorrow to review it. And it
has *gasp* jQuery installed! I'm sure I'll lose the room when I hear
that.

Thomas
 
D

David Mark

Eloquent indeed. How about you go see yours? She's right upstairs, I'm

You wouldn't make a very good detective either.
sure. Now you see how it feels: You tell me that I'm unhirable, and
I'll _show_ that you are (not that anybody would want your attitude on
their team). I mean, if that's your work experience going back to
2002, that's pretty sad:

http://www.google.com/search?hl=en&as_q=&as_epq=&as_oq=&as_eq=&num=10...

I must have missed Sixty Minutes.
Anyway, enough time wasting. I have to wrap up changes on a website

Like posting links to my mock-ups?
since the client's board will be meeting tomorrow to review it. And it
has *gasp* jQuery installed! I'm sure I'll lose the room when I hear
that.

I think the client, board and meeting are all in your head.
 
E

Eric Bednarz

Thomas Allen said:
Now you see how it feels:

I can still remember when this group used to be about browser scripting.
You tell me that I'm unhirable, and
I'll _show_ that you are

All you are about to show is that you cannot trim redundant parameters
from an URI reference.
(not that anybody would want your attitude on
their team)

I know a lot of teams that have nothing *but* attitude, was that
announcement supposed to be a compliment or an insult?

JR is the ESR of J(ava)Script.

(now *that’s* bashing :)
 
T

Thomas Allen

I can still remember when this group used to be about browser scripting.


All you are about to show is that you cannot trim redundant parameters
from an URI reference.


I know a lot of teams that have nothing *but* attitude, was that
announcement supposed to be a compliment or an insult?


JR is the ESR of J(ava)Script.

(now *that’s* bashing :)

Sorry for the OT, but I certainly didn't start it. I'll try not to get
heated by D.Mark's silliness in the future.

Thomas
 
T

Thomas 'PointedEars' Lahn

kangax said:
Thomas said:
Garrett Smith wrote: [...]
10.1.3 guarantees that missing parameters get value |undefined|.
No, it doesn't:

undefined = "foo";

What are you talking about? Overwriting `undefined` property of a global
object does not affect the value of arguments missing from the
function's arguments list. They still get assigned `undefined` value to:

undefined = 'foo';
(function(x){ return typeof x; })(); // "undefined"

Perhaps, you meant something else?

Yes, I meant that *because* `undefined' is not read-only, it cannot be used
for a bullet-proof test whether or not an argument was passed. Your test
here proves exactly what I mean to say: that Garrett's suggestion before is
error-prone because ES3F, 10.1.3 guarantees that missing arguments are
getting the `undefined', but using the `undefined' literal/property in
source code is _not_ a reliable (or backwards-compatible) means to test
against that.
You forgot about `in` operator ;)

No, I didn't.
(and `Object.prototype.hasOwnProperty`, of course)

I said "bullet-proof" for good reasons, among them:

<http://pointedears.de/scripts/test/es-matrix/#o>
Variable can also be checked for an `undefined` value by comparing it to
the result of an expression produced by `void` operator, which is
guaranteed to return `undefined` value.http://PointedEars.de/es-matrix#in
ACK

There are a number of other ways of course, but I can't see when `typeof`
would not be sufficient.

Either you misunderstood, or perhaps my wording was a bit confusing/confused.


PointedEars
 
J

Jorge

Yes, I meant that *because* `undefined' is not read-only, it cannot be used
for a bullet-proof test whether or not an argument was passed.  Your test
here proves exactly what I mean to say: that Garrett's suggestion before is
error-prone because ES3F, 10.1.3 guarantees that missing arguments are
getting the `undefined', but using the `undefined' literal/property in
source code is _not_ a reliable (or backwards-compatible) means to test
against that.

There are currently a million things that are writable, mutable or
just shadowable in JS. Most of them, if not all.

You're -obviously- not going to be the one overwriting the global
undefined in your own page, not even by accident, so it must be that
some evil code that's not yours makes its way into it.

But when/if that happens you're lost anyways, the attack surface is
so (currently, inevitably) big that there's absolutely *nothing* you
can do to avoid the worst.

So why would you care about this (just another) inane bit in
particular ?

If writing |varName === void(0)| or |typeof varName === 'undefined'|
makes you feel any better/safer that writing |varName === undefined|
you're a complete idi^H^H^H^Hly mistaken, AFAICS.
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Mmm, no.
I know for certain that at least one ES3 implementation optimizes away
the arguments object creation, and simple tests suggests that others
do the same (speed of calling a function decreases if it contains an
identifier named "arguments", even if it isn't used).

Your argument is inconclusive, of course, because if the arguments object is
referred to in a non-obvious fashion (i.e., not simply `arguments') it would
need to be created during execution and thus cost during execution. And to
determine before execution whether it is referred to in a non-obvious
fashion is even more expensive.
The specification is not an implementation specification, but a
behavioral specification. If the arguments object isn't referenced,
then the behavior doesn't change if it's not created.

Nonsense. The Specification says the argument object MUST be created when
control enters an execution context for function code, and then it goes on
to describe the features this object is to provide. Period. It makes no
statement about how this object can be referred to or when it can be
garbage-collected.

And there are good reasons why it does this:

function foo()
{
var s = "a"
+ /%72%67%75%6d%65%6e%74/.source.replace(
/%([\da-f]+)/g,
function(m, p1) {
return String.fromCharCode(parseInt(p1, 16)));
})
+ "s";

/* ReferenceError? */
eval('try { window.external[foo[0]]; }'
+ 'catch (e) { throw new Error("Inaccessible first argument"); }');
}

foo("addFavorite");

It is a deliberately extreme example, of course, because those help to see
the bigger picture.
Try it! Add the following to a page and try it in different browsers:

That they do it is no indication that what they do is the correct approach
or even standards-compliant.


PointedEars
 
M

Matt Kruse

Your argument is inconclusive, of course, because if the arguments objectis
referred to in a non-obvious fashion (i.e., not simply `arguments') it would
need to be created during execution and thus cost during execution.  And to
determine before execution whether it is referred to in a non-obvious
fashion is even more expensive.

That is probably true. But, the majority of cases where arguments is
used will not do so in a non-obvious manner. So for the vast majority
of typical cases, the optimization leads to a performance improvement.
Only in edge cases is there a (potential) performance hit. As is the
case in most optimizations, there is a trade-off, and it seems like
this is a good one.
Nonsense.  The Specification says the argument object MUST be created when
control enters an execution context for function code

The only requirement is that the calling code should always be able to
act is if it had been created. That is, the code doesn't need to check
to see if it's there - it can assume it is. However, if the code never
refers to the arguments and it, in fact, was never created, then there
is no loss in functionality!

If the code has no way to determine whether the arguments object has
been created, and the behavior is always consistent with what would be
expected if it _had_ been created, then the requirement is met.

Clearly, these optimizations exist in current browsers and lead to
performance increases without any problems. So I guess I'm glad you're
not in charge of implementing js in a browser ;)

Matt Kruse
 
T

Thomas 'PointedEars' Lahn

Matt said:
Thomas said:
Your argument is inconclusive, of course, because if the arguments object is
referred to in a non-obvious fashion (i.e., not simply `arguments') it would
need to be created during execution and thus cost during execution. And to
determine before execution whether it is referred to in a non-obvious
fashion is even more expensive.

That is probably true. But, the majority of cases where arguments is
used will not do so in a non-obvious manner. So for the vast majority
of typical cases, the optimization leads to a performance improvement.
Only in edge cases is there a (potential) performance hit. As is the
case in most optimizations, there is a trade-off, and it seems like
this is a good one.
Maybe.
Nonsense. The Specification says the argument object MUST be created when
control enters an execution context for function code

The only requirement is that the calling code should always be able to
act is if it had been created. [...]

That is _not_ what the ECMAScript Language Specification, Edition 3 Final
says. It describes what conforming implementations MUST implement, and what
they also MAY implement. What that means is made very clear, starting with
its Conformance section.

It is (like any other specification) an *implementation* specification,
_not_ a behavioral implementation; believe it or not.

If the arguments object is not created in some implementations when not
obviously being referred to then that may be considered an optimization (and
a doubtful one at that), but it is _not_ standards-compliant behavior at
this point.

And if a developer relies on that non-standard feature in spite of a
standards-compliant, more compatible and more reliable solution, like
suggested here, then that must be called just plain stupid.


PointedEars
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top