<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programmer Luddite &#187; javascript</title>
	<atom:link href="https://programmerluddite.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://programmerluddite.com</link>
	<description>The Blog of Barney Boisvert, Software Craftsman</description>
	<lastBuildDate>Tue, 26 Apr 2022 13:20:50 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.9.1</generator>
	<item>
		<title>Almost @Decorate</title>
		<link>https://programmerluddite.com/2017/08/almost-decorate/</link>
		<comments>https://programmerluddite.com/2017/08/almost-decorate/#comments</comments>
		<pubDate>Fri, 25 Aug 2017 19:56:18 +0000</pubDate>
		<dc:creator><![CDATA[barneyb]]></dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://programmerluddite.com/?p=231</guid>
		<description><![CDATA[This is a handy little snippet I&#8217;ve been using to emulate ES7&#8242;s decorators without needing to depend on a non-standard syntax, nor wire in extra Babel (or whatever) plugins. Or even use more than ES3, once the function itself is &#8230; <a href="https://programmerluddite.com/2017/08/almost-decorate/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>This is a handy little snippet I&#8217;ve been using to emulate ES7&#8242;s decorators without needing to depend on a non-standard syntax, nor wire in extra Babel (or whatever) plugins. Or even use more than ES3, once the function itself is transpiled.</p>
<pre>const decorate = (...decorators) =&gt; component =&gt;
    decorators.reduce((c, d) =&gt; d(c), component);</pre>
<p>Consider this ES7 decorated component (using the syntax as of 2017-08-25):</p>
<pre>@MyDecorator(/* my config */)
@OtherDecorator(/* other config */)
class Fred {}
export default Fred;</pre>
<p>Without decorators, it looks like this:</p>
<pre>class Fred {}
export default OtherDecorator(/* other config */)(
    MyDecorator(/* my config */)(
        Fred
    )
);</pre>
<p>I find this hard to read, even before complicated configuration (functions returning objects of functions, anyone?). Even if you play around with different line breaks, indentation, and intermediate variables, it&#8217;s never super clean. Also, the nesting is in the &#8220;reverse&#8221; order of how decorators apply. You often see a temp variable to fix this latter issue:</p>
<pre>class Fred {}
const wrappedFred = MyDecorator(/* my config */)(
    Fred
);
export default OtherDecorator(/* other config */)(
    wrappedFred
);</pre>
<p>Still a lot of cruft. Now if we use that <code>decorate</code> function up top, we get this:</p>
<pre>class Fred {}
export default decorate(
    MyDecorator(/* my config */),
    OtherDecorator(/* other config */)
)(Fred);</pre>
<p>Not as nice as today&#8217;s candidate ES7 syntax, but only barely. And it&#8217;s also completely transparent, unlike the Babel plugin (found at <a title="https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy/blob/v1.3.4/src/index.js" href="https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy/blob/v1.3.4/src/index.js">https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy/blob/v1.3.4/src/index.js</a> for the masochists among us).</p>
<p><strong>Update:</strong> if you don&#8217;t have an ES6 transpiler and need ES3, here it is:</p>
<pre>function decorate() {
    var l = arguments.length, decorators = Array(l);
    for (var i = 0; i &lt; l; i++) {
        decorators[i] = arguments[i];
    }
    return function(component) {
        return decorators.reduce(function(c, d) {
            return d(c);
        }, component);
    };
};</pre>
]]></content:encoded>
			<wfw:commentRss>https://programmerluddite.com/2017/08/almost-decorate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>$emit-ing Events from Outside AngularJS</title>
		<link>https://programmerluddite.com/2017/03/emit-events-outside-angular/</link>
		<comments>https://programmerluddite.com/2017/03/emit-events-outside-angular/#comments</comments>
		<pubDate>Mon, 06 Mar 2017 17:45:18 +0000</pubDate>
		<dc:creator><![CDATA[barneyb]]></dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://programmerluddite.com/?p=226</guid>
		<description><![CDATA[Talk to ARMUS]]></description>
				<content:encoded><![CDATA[<p>Talk to ARMUS</p>
]]></content:encoded>
			<wfw:commentRss>https://programmerluddite.com/2017/03/emit-events-outside-angular/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
