Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1562636.Ll19dNWgsJ@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sun, 02 Dec 2012 19:39:59 +0100 User-Agent: KNode/4.4.11 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+&. <5462881.fXVs31Ce0e@PointedEars.de> <5551855.fYihAxhPQP@PointedEars.de> <2075691.byxZkP05Om@PointedEars.de> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 58 NNTP-Posting-Date: 02 Dec 2012 19:40:00 CET NNTP-Posting-Host: 001e1de2.newsspool1.arcor-online.net X-Trace: DXC=iaiKWM`8X37d8Nb@@ZG@b=ic==]BZ:af>4Fo<]lROoR1nkgeX?EC@@0IRTKiQcV`14DZm8W4\YJNAk9?fi>3?6= X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:17417 Thomas 'PointedEars' Lahn wrote: > Consider this instead: > > for (var key in params) > { > if (params.hasOwnProperty(key)) > { > // … > } > } > > The least you should do is to cache the value of > `Object.prototype.hasOwnProperty', maybe even in a bound variable of a > closure (as can be seen in JSX), if you do not call it through the > prototype chain. > > /* > * Or `Object.prototype', but `{}' appears to have a shorter > * scope chain, which might win over the longer prototype chain > */ > var _hasOwnProperty = {}.hasOwnProperty; > > for (var key in params) > { > if (_hasOwnProperty.call(params, key)) > { > // … > } > } > > It might be useful to combine these: > > var hasHasOwnProperty = ^^^^^^^^^^^^^^^^^ That must _hasOwnProperty. I wrote the hasHasOwnProperty() approach later but it did not work well with boolean expressions, so I reverted it (only partially, apparently). > (typeof params.hasOwnProperty == "function") > ? params.hasOwnProperty > : {}.hasOwnProperty; > > for (var key in params) > { > if (_hasOwnProperty.call(params, key)) > { > // … > } > } PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs,