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)

exists.patch (1.9 KB) - added by dante 10 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 11 years ago by lipik

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 with typeof obj["val"] != "undefined", or even val in obj.

I am not sure that the latter is supported in IE6, though.

comment:2 Changed 11 years ago by James Burke

Milestone: tbd1.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 11 years ago by James Burke

Resolution: fixed
Status: newclosed

(In [17325]) Fixes #9001: clarify docs on the purpose of dojo.exists.

Changed 10 years ago by dante

Attachment: exists.patch added

comment:4 Changed 10 years ago by dante

Resolution: fixed
Status: closedreopened
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 James Burke

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 dante

Milestone: 1.41.6

comment:7 Changed 10 years ago by dante

Resolution: fixed
Status: reopenedclosed

(In [22878]) fixes #9001 - thanks cowbow for the initial filing. going with full and negligible byte increase approved. of course you're \!strict

Note: See TracTickets for help on using tickets.