Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1513771.HS4XPLjgls@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Tue, 03 Sep 2013 00:03:21 +0200 User-Agent: KNode/4.10.5 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <0.c54068976de565cd12fc.20130902215206BST.877gezuetl.fsf@bsb.me.uk> MIME-Version: 1.0 Lines: 92 NNTP-Posting-Date: 03 Sep 2013 00:03:24 CEST NNTP-Posting-Host: 84d90979.newsspool3.arcor-online.net X-Trace: DXC=H:Mo>XP2Ac0^cW`WBF>WQ1DZm8W4\YJN kj 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 : >> >> ...while still preserving the *conditional* evaluation of the >> clause? (Here 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 : > > If HAS returns o.p when o.hasOwnProperty('p'), then you can avoid the > conditional > > HAS(o, 'p') || 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') || would be > 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): 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: must be excluded. The proprietary, considerably safe "p" in o&&!("p" in o.__proto__)?o.p: would not be shorter than o.hasOwnProperty('p')?o.p: And the “in” operator should be forbidden with host objects because it uses [[HasProperty]]. Much depends on what is meant by “JavaScript”: -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.