Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #21150
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: (golf-ish) Conditional object property access |
| Date | 2013-09-02 23:10 +0200 |
| Organization | albasani.net |
| Message-ID | <l02us6$42f$1@news.albasani.net> (permalink) |
| References | <l02s3s$fdt$1@reader1.panix.com> |
On 2013-09-02 22:23, kj wrote:
> 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.)
[...]
> 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).
I think that's about as close as you can get without special syntactic
sugar. If only the value of truthy properties is of interest, this form
could make a shorter alternative:
HAS(o, 'p') || <EXPR>
function HAS (obj, prop)
{
if (obj.hasOwnProperty(prop)) {
return obj[prop];
}
}
Golf entertainment aspect aside, is the verbosity of hasOwnProperty()
actually bothering you? This method tends to be used more in libraries
than ad-hoc scripts, so the ugliness should stay well hidden most of the
time.
In a misguided attempt to reduce the download size by increasing the
efficiency of minimizers, I once used something like this (not just for
hasOwnProperty):
// large library with many occurrences of hasOwnProperty etc:
(function () {
var _hasOwnProperty = "hasOwnProperty";
...
if (obj[_hasOwnProperty](prop)) {
...
})();
"_hasOwnProperty" can then be minified/obfuscated to something like "a",
which I thought was a good thing at the time. Honestly, the main effect
was to make the code less readable for the dubious benefit of saving a
few bytes. Not recommended.
I suspect that any highly golfed solution you may discover will suffer
from similar readability problems.
- stefan
Back to comp.lang.javascript | Previous | Next — Previous 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