Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #21148
| From | kj <no.email@please.post> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | (golf-ish) Conditional object property access |
| Date | 2013-09-02 20:23 +0000 |
| Organization | none |
| Message-ID | <l02s3s$fdt$1@reader1.panix.com> (permalink) |
(WARNING: This is a "JavaScript golf"-type query, of little practical
import; just for those who get into this sort of thing...)
Quick version
=============
How to shorten the generic JavaScript expression
o.hasOwnProperty('p') ? o.p : <EXPR>
...while still preserving the *conditional* evaluation of the <EXPR>
clause? (Here <EXPR> stands for an arbitrary JavaScript
expression.)
----------------------------------------------------------------------
Leisurely version
=================
I'm dealing with JavaScript code where expressions having the form
above appear *all over*.
Given the frequency of this pattern, it would be nice if JS had
some special, and more succinct, syntax for it, e.g., something
like:
o['p':<EXPR>]
...where the <EXPR> "clause" would be evaluated (and returned as the
value of the whole expression) *only if* o.hasOwnProperty('p') is
false[1].
Absent such syntactic help, how can one shorten the pattern given
above?
The best that my noob-level JS can come up with is to define a helper
function `HAS` (in a suitably restricted scope) to replace the verbose
built-in hasOwnProperty, so that one can at least write:
HAS(o, 'p') ? o.p : <EXPR>
At the end of this post I give other alternatives I could think of,
but they are all either *more* verbose than what they try to replace,
or have some other weakness (e.g. failure to delay the evaluation of
the <EXPR> clause).
For the more expert JS coders out there: can one do better?
----------------------------------------------------------------------
Below are my various attempts:
o.hasOwnProperty('p')?o.p:<EXPR> // the one to beat
o['p':<EXPR>] // hypothetical/theo-
// retical minimum?
HAS(o,'p')?o.p:<EXPR> // my disappointing best
GET(o,'p',<EXPR>) // <EXPR> evaluated
// unconditionally
GET2(o,'p',function(){return <EXPR>}) // ambiguous when the
// value of o.p may be
// a function
GET3(o,'p',T(function(){return <EXPR>})) // T for Thunk
function HAS(obj, prop) {
return obj.hasOwnProperty(prop);
}
function GET(obj, prop, alt) {
return HAS(obj, prop) ? obj[prop] : alt;
}
function GET2(obj, prop, alt) {
return HAS(obj, prop) ? obj[prop] : alt();
}
function Thunk (fn) { this.fn = fn }
function T(fn) { return new Thunk(fn) }
function GET3(obj, prop, alt) {
return HAS(obj, prop) ? obj[prop] :
alt instanceof Thunk ? alt.fn() : alt;
}
----------------------------------------------------------------------
[1] The "delayed", or, more precisely, *conditional*, evaluation of
the <EXPR> clause is an important aspect of the original
expression's semantics, and is therefore one that any alternative
formulation should retain. I point this out because the first
alternative formulation that I came up with, the one using the
helper function `GET`, turns out to fall short in this respect.
Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread
(golf-ish) Conditional object property access kj <no.email@please.post> - 2013-09-02 20:23 +0000
Re: (golf-ish) Conditional object property access Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-09-02 21:52 +0100
Re: (golf-ish) Conditional object property access Stefan Weiss <krewecherl@gmail.com> - 2013-09-02 23:11 +0200
Re: (golf-ish) Conditional object property access Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-03 00:03 +0200
Re: (golf-ish) Conditional object property access Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-09-02 23:19 +0100
Re: (golf-ish) Conditional object property access Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-03 01:59 +0200
Re: (golf-ish) Conditional object property access Stefan Weiss <krewecherl@gmail.com> - 2013-09-02 23:10 +0200
csiph-web