Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Is this style ok? (bind) Supersedes: <2541201.q52ZUWV9dV@PointedEars.de> Date: Wed, 02 Jul 2014 05:55:05 +0200 Organization: PointedEars Software (PES) Lines: 102 Message-ID: <3644428.WxxhaMYl5t@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1404273307 20747 eJwFwQkRwEAIBDBLPWBhkMO3/iU0gfrzCXO4gWDxoiVLMMeueeOCslO/rRXd0NwoxiCbX/9DuBJ9 (2 Jul 2014 03:55:07 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Wed, 2 Jul 2014 03:55:07 +0000 (UTC) User-Agent: KNode/4.12.4 Cancel-Key: sha1:A6ncK/sNjJZrWEGf7Ha94rTqe4o= X-User-ID: eJwFwQkBwDAIA0BLfKFBDu3Av4TdwVPznUhkYLHkRHvhYuTLQTL9uXMXdemiH1qsxKbcpG6vvUMl9Uhn1A85lhRX Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. A student in my JavaScript course today showed me his code. ^^^^^^^^^^^^^^^^^^^^^^^ OMG. > It looked like: > > function v01(){ console.log( "http://example.com/aqeritugh" ); } > function v02(){ console.log( "http://example.com/eargqqrgh" ); } > function v03(){ console.log( "http://example.com/grqoqqrug" ); } > ... > a.b.c01 = v01; > a.b.c02 = v02; > a.b.c03 = v03; > ... > > . »console.log« above is just an example. In reality, he called > another function, and his functions where a little bit longer. > > He asked me: »How can I avoid to have to define so many > different functions?« > > My solution was: > > function v(){ console.log( "http://example.com/" + this ); } > ... > a.b.c01 = v.bind( "aqeritugh" ); > a.b.c02 = v.bind( "eargqqrgh" ); > a.b.c03 = v.bind( "grqoqqrug" ); > ... > > Is it good style to use »bind« in this way to implement > a kind of function macro? No. Function.prototype.bind() was only introduced with ECMAScript Edition 5 and it creates a new Function instance when is called. You would end up with 4+ Function instances, since the “v” would prevent the original from being garbage-collected. > When I am thinking about it now, another solution might be: > > function w( text ) > { return function(){ console.log( "http://example.com/" + text ); }} > ... > a.b.c01 = w( "aqeritugh" ); > a.b.c02 = w( "eargqqrgh" ); > a.b.c03 = w( "grqoqqrug" ); > ... > > Is this better or worse? Better. More compatible, more efficient, more obvious. Of course, this begs the question why there would be so many functions in the first place, and why they would not use just one function with a parameter. function y (text) { console.log("http://example.com/" + text); } And of course, your code style still sucks big time, and I can only hope for your students that you do not teach it to them. In fact, at the moment and for some years to come they would be much better off with another teacher, one experienced in the field. But such explains some of the quality of code and the misconceptions we have had to be exposed to here and elsewhere… Bock, Gärtner. > (I know that > > a.b.c01 = w( "aqeritugh" ); > a.b.c02 = w( "eargqqrgh" ); > a.b.c03 = w( "grqoqqrug" ); > ... > > still contains redundancy, one could write > > [ "aqeritugh", "eargqqrgh", "grqoqqrug" ].forEach > ( function( e, i, a ){ a.b[ "c0" + i ]= w( e )); } ) But one should not. Again, Array.prototype.forEach() is an ES 5 feature, and again, efficiency is reduced by n additional function calls and a longer effective scope chain. Instead, write for (var a2 = ["aqeritugh", "eargqqrgh", "grqoqqrug"], i = 0, len = a2.length; i < len; ++i) { a.b["c0" + a2[i]] = w(e); } or use ”let” if compatibility is not an issue. -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not Cc: me. / Bitte keine Kopien per E-Mail.