Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #25183
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Is this style ok? (bind) |
| Date | 2014-07-02 05:55 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <3644428.WxxhaMYl5t@PointedEars.de> (permalink) |
| References | <bind-20140702005726@ram.dialup.fu-berlin.de> |
Stefan Ram wrote:
> 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: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next | Find similar | Unroll thread
Re: Is this style ok? (bind) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-07-02 05:55 +0200
csiph-web