TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

JavaScript Getter-Setter Pyramid

142 pointsby adgasfover 6 years ago

14 comments

snekover 6 years ago
This article seems to be making up its own terminology and patterns. In JS, a getter is a function bound to a property that is called when that property is accessed. (mdn.io/getter) and a setter is a function bound to a property that is called when the property is set (mdn.io/setter). In JS there isn't any real differentiation you need to make between a function with 0 arguments, a function with 1 argument, and a function with N arguments. They're all functions with some arity that you can call.
评论 #18716473 未加载
评论 #18716105 未加载
评论 #18723682 未加载
评论 #18717564 未加载
评论 #18715996 未加载
评论 #18721904 未加载
acjohnson55over 6 years ago
This is a pretty interesting ontology of one path generalization.<p>I find the getter&#x2F;setter nomenclature a little imprecise. It makes me think of interacting with fixed state, rather than input and output into completely abstract &quot;systems&quot; on the other side of the function calls. To me, provider&#x2F;consumer seems a bit more on-the-mark.<p>It seems to me that there are other paths of generalization you could follow, as well (e.g. <a href="https:&#x2F;&#x2F;leanpub.com&#x2F;combinators" rel="nofollow">https:&#x2F;&#x2F;leanpub.com&#x2F;combinators</a>). Even though we all know that you can model...well...<i>anything</i> with functions, it&#x27;s still enlightening and mindbending to explore the patterns for actually doing so.
megousover 6 years ago
&gt; Writing the above code example with none of the abstractions in the Getter-Setter Pyramid requires more amount of code, which is also harder to read.<p><pre><code> let usersRange = (start, end, result, doneCb, errorCb) =&gt; { let xhr = new XMLHttpRequest(); xhr.responseType = &#x27;json&#x27;; xhr.open(&#x27;GET&#x27;, &#x27;http:&#x2F;&#x2F;jsonplaceholder.typicode.com&#x2F;users&#x2F;&#x27; + start) xhr.send(); xhr.onerror = ev =&gt; errorCb(); xhr.onload = ev =&gt; { result.push(xhr.response); if (start &lt; end) return usersRange(start + 1, end, result, doneCb, errorCb); done(result); } }; usersRange(0, 10, [], &#x2F;* completion callbacks *&#x2F;); </code></pre> Simpler, not really harder to read (half of it is just boilerplate around XHR setup, which is abstractable away). The meat of it is onload handler.
评论 #18721165 未加载
评论 #18715930 未加载
yuchiover 6 years ago
Similar in concept to the General Theory of Reactivity, GTOR: <a href="https:&#x2F;&#x2F;github.com&#x2F;kriskowal&#x2F;gtor&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kriskowal&#x2F;gtor&#x2F;</a>
SeanLukeover 6 years ago
I&#x27;m interested in this simple closure code from the article:<p><pre><code> function getGetNext() { let i = 2; return function getNext() { const next = i; i = i * 2; return next; } } </code></pre> As a lisp coder [and not a JS coder], I&#x27;m wondering: why does the function getNext() have to be named? Why not just say something like:<p><pre><code> function getGetNext() { let i = 2; return function() { const next = i; i = i * 2; return next; } }</code></pre>
评论 #18716465 未加载
评论 #18720234 未加载
评论 #18721649 未加载
inglorover 6 years ago
There is a great talk by Erik Meijer (who invented Rx) showing this very neatly: <a href="https:&#x2F;&#x2F;channel9.msdn.com&#x2F;Events&#x2F;Lang-NEXT&#x2F;Lang-NEXT-2014&#x2F;Keynote-Duality" rel="nofollow">https:&#x2F;&#x2F;channel9.msdn.com&#x2F;Events&#x2F;Lang-NEXT&#x2F;Lang-NEXT-2014&#x2F;Ke...</a>
评论 #18717430 未加载
vqover 6 years ago
I thought this was going to be about lenses implemented in JavaScript, if it was I didn&#x27;t get it. The Haskell lens[1] package (which implements a type of lenses referred to as van Laarhoven lenses after their discoverer Twan van Laarhoven) builds up a hierarchy (pyramid if you will) of lenses with varying degrees of generality and power.<p>[1]: <a href="http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;lens-4.17#readme" rel="nofollow">http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;lens-4.17#readme</a>
nenadgover 6 years ago
There is no copyright on terms &#x27;getter&#x27;, &#x27;setter&#x27; etc.<p>For the first part I didn&#x27;t like the article since it&#x27;s somewhere between fp and oop, vague or foggy. But it&#x27;s just my interpretation based&#x2F;biased with previous knowledge.<p>Once I assumed the article is conceptual and in harmony with it&#x27;s definitions, everything got much more sense.<p>I like it.
kowdermeisterover 6 years ago
My OCD kicked in at the end of the article looking at that pyramid. He should have put a triangle on top :)
评论 #18716177 未加载
评论 #18716362 未加载
tylerjwilk00over 6 years ago
I&#x27;m sure the author knows more about the topic than me. However, it reads like someone trying to rationalize something that isn&#x27;t quite rational.<p>IE: Walking into an old antique store and attempting to explain the genius logic behind it&#x27;s organization. Shortly after the owner comes out and says, &quot;No, I just throw shit wherever.&quot;
whyonearthover 6 years ago
&gt; The cornerstone of JavaScript is the function.<p>JavaScript is specified in terms of objects, has richer facilities for dealing with objects than functions, functions are objects but not vice-versa. So, objects are the cornerstone. Callers are free to ignore a function&#x27;s arity even.
评论 #18720646 未加载
azangruover 6 years ago
Is this the write-up of his talks?<p>I remember this one, from the Uphill Conference: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=fdol03pcvMA" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=fdol03pcvMA</a>
评论 #18717116 未加载
vernieover 6 years ago
JavaScript was a mistake.
gamblerover 6 years ago
JavaScript is truly the new Java. It even has design pattern articles now.