Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ken-okabe/df22bb59f595de5e02d9 to your computer and use it in GitHub Desktop.
Save ken-okabe/df22bb59f595de5e02d9 to your computer and use it in GitHub Desktop.
JavaScriptはオブジェクト指向だろ?wとかいう、ありがちな誤解と大嘘 関数型言語
<h1 id="javascriptはオブジェクト指向だろwとかいうありがちな誤解と大嘘-関数型言語">JavaScriptはオブジェクト指向だろ?wとかいう、ありがちな誤解と大嘘 関数型言語</h1>
<p>この記事はもともと、 <br>
てんでバラバラに語られ解説されてしまうことが非常に多い、</p>
<p>非同期、 <br>
Ajax(Asynchronous JavaScript And XML)、 <br>
イベント駆動、 <br>
宣言型プログラミング、 <br>
関数型プログラミング、 <br>
FRP、 <br>
参照透過、 <br>
イミュータブル</p>
<p>はぜんぶ同じパラダイムにある、ってことを書くつもりで、 <br>
非常にかるーい気持ちで書き始めたんですが、 <br>
やっぱ路線変更しました。</p>
<p>以上のことも重要なので、続けて書きますが、 <br>
まず主題として、 <br>
JavaScriptはオブジェクト指向だろ?wとかいう、 <br>
しょっちゅう筆者が目にする訳知り顔の連中によるありがちな誤解、 <br>
関数型言語</p>
<h2 id="関数型パラダイムをメインストリームにしたのはwebの発達">関数型パラダイムをメインストリームにしたのはWebの発達</h2>
<p>チャーチらのラムダ計算の研究、 <br>
ジョン・マッカーシーによるLISPの研究、 <br>
Haskellの実装などを経て、 <br>
2010年代になりようやくソフトウェア開発のメインストリームとしての <br>
関数型プログラミングにパラダイムシフトしている状況なのですが、 <br>
この潮流をつくったのはWebの発達です。</p>
<p>筆者が観察するに、このWebの発達を関数型パラダイムシフトとした先見の明があるキープレイヤーは</p>
<ul>
<li>JavaScript言語の開発者</li>
<li>Gooleの技術者</li>
</ul>
<p>です。</p>
<h2 id="javascript">JavaScript</h2>
<h2 id="webのhistoryの話をしよう">WebのHistoryの話をしよう</h2>
<p>筆者は、ものごとの本質を理解するときには、 <br>
ものごとの「なりたち」「経緯」「History」のことをめちゃくちゃ重視します。 <br>
なぜならば、ものごとの「なりたち」「経緯」「History」には、 <br>
必ず、そのものごとに関与した「人」の存在があり、 <br>
その「人」の「考え方」「考えた理路」「思想」が見えてくるからです。 <br>
それはものごとの本質を理解するときに欠かせない。</p>
<p>多くの人は、新しいものごとに触れるとき <br>
「3行でまとめろ」 <br>
みたいなことを要求して平然としています。</p>
<p>まあそこそこ忙しいんでしょうし、自分の頭を使うのが面倒なんでしょう。 <br>
単純に頭が足りないだけなのかもしれない。</p>
<p>こういうのは、いわば「情報の要約を他人任せにしている」ってことなんですね。 <br>
たしかにWikipediaとかYahoo知恵袋だとか集合知は機能しますけど、 <br>
まあ、特にWikipediaの「他人任せの情報の要約」って、 <br>
あんまり役に立たないことがとても多い。</p>
<p>さほど利口でない誰かの情報処理の影響が大きい要約かもしれない、 <br>
第二次情報だからです。</p>
<p>実際に、日本語版Wikipediaの <br>
<a href="http://ja.wikipedia.org/wiki/JavaScript">http://ja.wikipedia.org/wiki/JavaScript</a> <br>
にはこうあります。</p>
<blockquote>
<p>パラダイム: マルチパラダイム</p>
<p>JavaScript(ジャヴァスクリプト)とは、プログラミング言語のひとつである。Javaと名前が似ているが、異なるプログラミング言語である(後述の#歴史を参照)。 <br>
<strong>オブジェクト指向のスクリプト言語であることを特徴とする。</strong> <br>
実行環境が主にウェブブラウザに実装され、動的なウェブサイト構築や、リッチインターネットアプリケーションなど高度なユーザインタフェースの開発に用いられる。</p>
</blockquote>
<p>こういうのを鵜呑みにしてはいけません。</p>
<p>英語版Wikipedia <br>
<a href="http://en.wikipedia.org/wiki/JavaScript">http://en.wikipedia.org/wiki/JavaScript</a> <br>
はもうちょっとマシで、</p>
<blockquote>
<p>Paradigm Multi-paradigm: scripting, object-oriented (prototype-based), imperative, <strong>functional</strong></p>
<p>JavaScript is classified as a prototype-based scripting language with dynamic typing and first-class functions. This mix of features makes it a multi-paradigm language, supporting object-oriented,[6] imperative, and <strong>functional</strong>[1][7] programming styles.</p>
</blockquote>
<p>それがマルチパラダイムで、 <br>
オブジェクト指向だけじゃなくてfunctionalであることも記述されています。</p>
<p>別にWikipediaが使えない批判をしたいわけじゃなくて、 <br>
そういう文句を言うならば自分で編集しろみたいな「応酬」がなされるわけです。</p>
<p>そうじゃなくって、 <br>
今ある他人任せの情報の要約っていうのは往往にして役に立たない、 <br>
ってことです。</p>
<p>Wikipediaは役に立ちます。 <br>
不正確な情報のなかに必ずFACTが多く含まれており、 <br>
情報の起点としては非常に有用。 <br>
そもそも「百科事典」っていうのはそういう風に使うものです。 <br>
「百科事典」の冒頭に書かれている「定義」であっても100%疑って、 <br>
自分で裏を取ることが必要です。</p>
<p>Wikipediaの記事に記載されている有用なFACTとして、 <br>
JavaScript言語の開発者は、ブレンダン・アイク <a href="http://en.wikipedia.org/wiki/Brendan_Eich">Brendan Eich</a> <br>
っていう人だってことがわかります。</p>
<p><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Brendan_Eich_Mozilla_Foundation_official_photo.jpg/330px-Brendan_Eich_Mozilla_Foundation_official_photo.jpg" alt="enter image description here" title=""></p>
<blockquote>
<p>Brendan Eich (/ˈaɪk/; born 1960 or 1961)[1] is an American technologist and <strong>creator of the JavaScript programming language</strong>. He co-founded the Mozilla project, the Mozilla Foundation and the Mozilla Corporation, and served as the Mozilla Corporation’s chief technical officer and briefly its chief executive officer.[2]</p>
</blockquote>
<p>日本語版より英語版のほうが編集数が多いだけ会って、 <br>
役に立つことは多いのでこのまま英語版をみると、</p>
<blockquote>
<p>Netscape and JavaScript[edit] <br>
<strong>He started work at Netscape Communications Corporation in April 1995. Hired originally to put Scheme “in the browser”</strong>,[5] Eich instead created a new language, JavaScript for the Netscape Navigator Web browser. The first version was completed in ten days in order to accommodate the Navigator 2.0 Beta release schedule,[5][6] and was called Mocha, which was later renamed LiveScript in September 1995 and later JavaScript in the same month.[7] Eich continued to oversee the development of SpiderMonkey, the specific implementation of JavaScript in Navigator, until 2011.[8]</p>
</blockquote>
<p>ものごとの「なりたち」「経緯」「History」を重視して、 <br>
その当事者本人の第一次情報に直接当たるのがもっとも確実で有効な方法です。</p>
<p>ブレンダン・アイクのブログを見ると面白い記事があります。 <br>
<a href="https://brendaneich.com/2008/04/popularity/">https://brendaneich.com/2008/04/popularity/</a></p>
<h3 id="popularity-3-april-2008">Popularity 3 April 2008</h3>
<blockquote>
<p>It seems (according to one <a href="http://javascript.crockford.com/popular.html">guru</a>, but coming from this source, it’s a left-handed compliment) that JavaScript is finally popular. <br>
どうも最近JavaScriptは遂にポピュラーになったようだ。 <br>
(あるグルによるとそうらしい、もっともこのグルの記事は単なる left-handed compliment【皮肉】だった)</p>
</blockquote>
<p>あるグルっていうのは <br>
JSLintを開発した、その筋では論争の多い<a href="http://en.wikipedia.org/wiki/Douglas_Crockford">Douglas Crockford</a>のことで、 <br>
( <br>
<a href="http://blog.node.ws/?p=1379">JSLintが独善的だってことで反発した人らがJSHintを作った</a></p>
<blockquote>
<p>君たちが苦しんでくれるほど、私の満足度は深まる —ダグラス・クロックフォード.</p>
</blockquote>
<p>)</p>
<p>ここでリンク引用されているguru=クロックフォードの記事 <br>
【<a href="http://javascript.crockford.com/popular.html">The World’s Most Misunderstood Programming Language Has Become the World’s Most Popular Programming Language</a> <br>
<strong>世界でもっとも誤解されているプログラミング言語が、</strong> <br>
<strong>世界でもっとも人気のプログラミング言語になった</strong>】</p>
<p>では、 </p>
<blockquote>
<p>JavaScript is an astonishing language, in the very worst sense. Its astonishing nature led to a very bad reputation. </p>
<p>JavaScriptは驚くべき言語だ、もっとも悪い意味で。 <br>
その驚くべき性質はたいへん悪い評判へと繋がっている。</p>
</blockquote>
<p>から、</p>
<blockquote>
<p>It is better to be lucky than smart.</p>
<p>Webブラウザの標準スクリプトになったからには、JavaScirptはそれほど悪い言語でもないわけだし、人気の標準言語となったことは重要なことなので、スマートさにこだわるよりも、勝ち馬に乗るほうが良い</p>
</blockquote>
<p>という結論に至るまで、 <br>
影響力の大きいguru=クロックフォードにさんざん皮肉を書き連ねてられており、JavaScriptを作ったブレンダン・アイクがカチンときて反撃したというのが、このブログエントリ「Popularity」が書かれた理由です。</p>
<p>このブレンダン・アイクによる記事は、 <br>
現在の関数型言語パラダイムシフトを分析する上で、 <br>
たいへん参照価値の高い一次情報であるので、ざっくり全文翻訳してみます。</p>
<hr>
<p>To me, a nerd from a tender age, this is something between a curse and a joke. (See if you are in my camp: isn’t the green chick hotter?)</p>
<p>幼少の頃からナードだった自分にとっては、これは呪いとジョークの間にある何かだと思う。 <br>
()←(訳)なんかジョーク書いてるけど意味不明</p>
<blockquote>
<p>So how did a language with such obvious deficiencies become the Sole Programming Language of the Web? <br>
一体どういう理由で、こんな不良品がWebの唯一のプログラミング言語になったのか? <br>
Brendan Eich convinced his pointy-haired boss at Netscape that the Navigator browser should have its own scripting language, and that only a new language would do, a new language designed and implemented in big hurry, and that no existing language should be considered for that role. <br>
ブレンダン・アイクは、Netscape社のツンツンヘアのボスに向かって、 <br>
「Navigatorブラウザは独自のスクリプト言語を持つべきで、新言語こそが必要だ。急ごしらえで設計され実装された新言語がその役割を担うべきで、他の既存の言語は考慮するに値しない。」 <br>
って説得したんだ。</p>
</blockquote>
<p>I don’t know why Doug is making up stories. He wasn’t at Netscape. He has heard my recollections about JavaScript’s birth directly, told in my keynotes at Ajax conferences. Revisionist shenanigans to advance a Microhoo C# agenda among Web developers?</p>
<p>なんで、Doug(Douglas Crockford)が、こんなつくり話をするのかわからない。彼は、Ajaxカンファレンスの自分の基調講演などで、私からJavaScriptの誕生秘話を直接聞いているはずなんだ。MicoなんとかのC#のアジェンダをWebデベロッパに向けて推進する馬鹿騒ぎに乗じたいのだろうか?</p>
<p>Who knows, and it’s hard to care, but in this week of the tenth anniversary of mozilla.org, a project I co-founded, I mean to tell some history.</p>
<p>まあいい、いちいち気にしていてもしょうがない。 <br>
ただ、今週は、私が共に創設したmozilla.orgの10周年でもあることだし、 <br>
その歴史的経緯について話してみようと思う。 </p>
<p>As I’ve often said, and as others at Netscape can confirm, I was recruited to Netscape with the promise of “doing Scheme” in the browser. At least client engineering management including Tom Paquin, Michael Toy, and Rick Schell, along with some guy named Marc Andreessen, were convinced that Netscape should embed a programming language, in source form, in HTML. So it was hardly a case of me selling a “pointy-haired boss” — more the reverse.</p>
<p>これまで往々にして私自身言ってきたとおり、またNetscapeの他の関係者も追認してくれるだろうが、私はNetscapeにブラウザで「Scheme言語をやる」という約束でリクルートされた。少なくとも、クライアント技術の経営陣、Tom Paquin, Michael Toy, Rick Schell, それからMarc Andreessen(Nesscapeの創設者)って名前の男は一緒になって、NetscapeはHTMLにソースフォームとしてプログラミング言語を組み込むべきだと確信していた。だから、「ツンツンヘアのボス」に自分が売り込んだってのはありえない。むしろ逆なんだ。</p>
<p>Whether that language should be Scheme was an open question, but Scheme was the bait I went for in joining Netscape. Previously, at SGI, Nick Thompson had turned me on to SICP.</p>
<p>なんでSchemeだったのか?ってのはよくわからなかった。しかし、Schemeこそが自分がNetscapeに参加しようと思った釣りエサ(動機)だった。 <br>
前職SGI(シリコン・グラフィックス社)でNick Thompsonが自分を <br>
SICP(Structure and Interpretation of Computer Programs)『計算機プログラムの構造と解釈』(*<strong>関数型言語</strong>のバイブルのひとつ。LISPの方言<strong>Scheme</strong>が用いられ、抽象化、再帰、インタプリタ、メタ言語的抽象といった計算機科学の概念の真髄が説明されている。)の信者にしてくれたからね。</p>
<p>What was needed was a convincing proof of concept, AKA a demo. That, I delivered, and in too-short order it was a fait accompli.</p>
<p>必要とされていたのは、説得力ある概念実証、つまりDEMOだった。 <br>
自分はすぐ実装した。そして既成事実となった。 <br>
  <br>
Of course, by the time I joined Netscape, and then transferred out of the server group where I had been hired based on short-term requisition scarcity games (and where I had the pleasure of working briefly with the McCool twins and Ari Luotonen; later in 1995, Ari and I would create PAC), the Oak language had been renamed Java, and Netscape was negotiating with Sun to include it in Navigator.</p>
<p>もっとも、自分がNestscapeに入る頃には、Javaの影響力が大きくなっていて、NetscapeはSunにJavaをブラウザに含められるように交渉をはじめていた。</p>
<p>The big debate inside Netscape therefore became “why two languages? why not just Java?” The answer was that two languages were required to serve the two mostly-disjoint audiences in the programming ziggurat who most deserved dedicated programming languages: the component authors, who wrote in C++ or (we hoped) Java; and the “scripters”, amateur or pro, who would write code directly embedded in HTML.</p>
<p>社内で大きな論争となった。 <br>
「なんで2つの言語なんだ?なんでJavaだけじゃないのか?」 <br>
答えは、2つのグループにわかれる顧客に提供するため。</p>
<p>Whether any existing language could be used, instead of inventing a new one, was also not something I decided. The diktat from upper engineering management was that the language must “look like Java”. That ruled out Perl, Python, and Tcl, along with Scheme. Later, in 1996, John Ousterhout came by to pitch Tk and lament the missed opportunity for Tcl.</p>
<p>I’m not proud, but I’m happy that I chose Scheme-ish first-class functions and Self-ish (albeit singular) prototypes as the main ingredients. The Java influences, especially y2k Date bugs but also the primitive vs. object distinction (e.g., string vs. String), were unfortunate.</p>
<p>Back to spring of 1995: I remember meeting Bill Joy during this period, and discussing fine points of garbage collection (card marking for efficient write barriers) with him. From the beginning, Bill grokked the idea of an easy-to-use “scripting language” as a companion to Java, analogous to VB‘s relationship to C++ in Microsoft’s platform of the mid-nineties. He was, as far as I can tell, our champion at Sun.</p>
<p>Kipp Hickman and I had been studying Java in April and May 1995, and Kipp had started writing his own JVM. Kipp and I wrote the first version of NSPR as a portability layer underlying his JVM, and I used it for the same purpose when prototyping “Mocha” in early-to-mid-May.</p>
<p>Bill convinced us to drop Kipp’s JVM because it would lack bug-for-bug compatibility with Sun’s JVM (a wise observation in those early days). By this point “Mocha” had proven itself via rapid prototyping and embedding in Netscape Navigator 2.0 , which was in its pre-alpha development phase.</p>
<p>The rest is perverse, merciless history. JS beat Java on the client, rivaled only by Flash, which supports an offspring of JS, ActionScript.</p>
<p>So back to popularity. I can take it or leave it. Nevertheless, popular Ajax libraries, often crunched and minified and link-culled into different plaintext source forms, are schlepped around the Internet constantly. Can we not share?</p>
<p>One idea, mooted by many folks, most recently here by Doug, entails embedding crypto-hashes in potentially very long-lived script tag attributes. Is this a good idea?</p>
<p>Probably not, based both on theoretical soundness concerns about crypto-hash algorithms, and on well-known poisoning attacks.</p>
<p>A better idea, which I heard first from Rob Sayre: support an optional “canonical URL” for the script source, via a share attribute on HTML5 </p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment