Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #21155

Re: (golf-ish) Conditional object property access

Message-ID <1513771.HS4XPLjgls@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-09-03 00:03 +0200
Subject Re: (golf-ish) Conditional object property access
Newsgroups comp.lang.javascript
References <l02s3s$fdt$1@reader1.panix.com> <0.c54068976de565cd12fc.20130902215206BST.877gezuetl.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse wrote:

> kj <no.email@please.post> writes:

Such anti-social address munging leads to at least one killfile entry.

>> (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.)
> <snip>
>> 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>
> 
> If HAS returns o.p when o.hasOwnProperty('p'), then you can avoid the
> conditional
> 
>   HAS(o, 'p') || <EXPR>

Incorrect.  There is a difference between an object having a property and 
converting the result of a property access to Boolean.  Assuming you meant

  function HAS (o, p)
  {
    if (o.hasOwnProperty(p)) return o[p];
  }

(where “HAS” is a very unwise identifier), if the value of o[p] was a false-
value, the result of

  HAS(o, 'p') || <EXPR>

would be

  <EXPR>

> Anything that uses functions to wrap EXPR for delayed execution suffers
> from the fact the ECMAScript uses such a long keyword, and that the
> syntax requires empty parentheses.  (I take it that one of the rules of
> this golf game is that the expression should be short.)

Harmony (ECMAScript Edition 6) is going to fix that with foo(bar => baz):

<http://wiki.ecmascript.org/doku.php?id=harmony:arrow_function_syntax>

That aside, the rules would also have to include that at “o” and “o.p” do 
not hold references to host objects because those objects do not need to 
implement [[HasProperty]] and [[Get]] the standard way (they may even throw 
exceptions, as long-time subscribers should know).

I do not think this expression can be shortened (if you count the called 
function as well), while being value-equivalent, as long as it must be 
*generic*.

The “in” operator could be used if it did not consider inherited properties, 
whereas hasOwnProperty() does not.  So the shorter

  "p" in o?o.p:<EXPR>

must be excluded.  The proprietary, considerably safe

  "p" in o&&!("p" in o.__proto__)?o.p:<EXPR>

would not be shorter than

  o.hasOwnProperty('p')?o.p:<EXPR>

And the “in” operator should be forbidden with host objects because it uses 
[[HasProperty]].

Much depends on what is meant by “JavaScript”:

<http://PointedEars.de/es-matrix>

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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