Opened 12 years ago
Closed 10 years ago
#9001 closed defect (fixed)
[patch][cla] dojo.exists behavior is a little confusing
Reported by: | ckbcowboy | Owned by: | James Burke |
---|---|---|---|
Priority: | high | Milestone: | 1.6 |
Component: | General | Version: | 1.3.0rc2 |
Keywords: | Cc: | ||
Blocked By: | Blocking: |
Description
I'm not a dojo user, but I noticed this today:
dojo.exists acts like it should be called dojo.truthy or dojo.methodExists.. it seems to be geared towards testing properties that will never be set to 0, false or empty string.
for example, in firebug console, on http://dojotoolkit.org/:
var foo = { a: 1, b: 0, c: false, d: null, e: '' }; console.log([ dojo.exists('a', foo), dojo.exists('b', foo), dojo.exists('foo.c'), dojo.exists('foo.d'), dojo.exists('foo.e'), dojo.exists('foo.f') ]); // logs: // [true, false, false, false, false, false] // but should probably log: // [true, true, true, true, true, false]
A fix would be to change !! to !== undefined.
- Cowboy
Attachments (1)
Change History (8)
comment:1 Changed 12 years ago by
comment:2 Changed 12 years ago by
Milestone: | tbd → 1.4 |
---|---|
Owner: | changed from anonymous to James Burke |
It looks like dojo.exists is just an artifact on top of dojo.getObject() and it is optimized for finding objects and methods on objects. I will update the docs to clarify its purpose.
Note that for the getting the behavior you want mentioned above, it is much easier (and faster) just to use the "in" operator in Javascript:
("d" in foo)
will return true.
comment:3 Changed 12 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
Changed 10 years ago by
Attachment: | exists.patch added |
---|
comment:4 Changed 10 years ago by
Resolution: | fixed |
---|---|
Status: | closed → reopened |
Summary: | dojo.exists behavior is a little confusing → [patch][cla] dojo.exists behavior is a little confusing |
-6 bytes, +11 bytes. teeny patch. unit tests pass, expanded unit tests.
comment:5 Changed 10 years ago by
I'm fine if the patch uses false instead of the /* falsey */0 thing, if the concern was number of bytes. I would rather have the code clearer. Other than that feel free to commit it.
comment:6 Changed 10 years ago by
Milestone: | 1.4 → 1.6 |
---|
comment:7 Changed 10 years ago by
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
I agree. Got bitten by this just last week. I vote that this be prioritized - fixed is simple, the
!!obj["val"]
check in exists() should be replaced withtypeof obj["val"] != "undefined"
, or evenval in obj
.I am not sure that the latter is supported in IE6, though.