<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Javascript on 答えを知りたい</title><link>https://hack.ironsand.net/tags/javascript/</link><description>Recent content in Javascript on 答えを知りたい</description><generator>Hugo -- 0.147.9</generator><language>en-US</language><lastBuildDate>Sun, 27 Oct 2019 15:42:54 +0900</lastBuildDate><atom:link href="https://hack.ironsand.net/tags/javascript/index.xml" rel="self" type="application/rss+xml"/><item><title>WebStormにあってRubyMineに存在しない機能</title><link>https://hack.ironsand.net/2019/functions-difference-between-webstorm-and-rubymine/</link><pubDate>Sun, 27 Oct 2019 15:42:54 +0900</pubDate><guid>https://hack.ironsand.net/2019/functions-difference-between-webstorm-and-rubymine/</guid><description>&lt;p>現在Railsの開発にRubyMineを使っているんだけど、Nuxtなどを触るためにJS専用のWebStormを買うべきか迷ってたので機能の違いを調べてみた。&lt;/p></description></item><item><title>NodeJSのREPLでctrl+r で過去の履歴を検索</title><link>https://hack.ironsand.net/2017/search-history-in-nodejs-repl-by-ctrl-r/</link><pubDate>Wed, 01 Nov 2017 06:28:12 +0000</pubDate><guid>https://hack.ironsand.net/2017/search-history-in-nodejs-repl-by-ctrl-r/</guid><description>&lt;p>&lt;code>bash&lt;/code>やRubyの&lt;code>pry&lt;/code>で&lt;code>ctrl+r&lt;/code>で使える reverse interactive search を&lt;code>node&lt;/code>のシェルでも使う方法の紹介です。&lt;/p>
&lt;h3 id="rlwrap-のインストール">rlwrap のインストール&lt;/h3>
&lt;p>&lt;code>rlwrap&lt;/code>が必要なので&lt;code>brew install rlwrap&lt;/code>でインストールしましょう。他の環境の場合は環境にあったパッケージマネージャでインストールしてください。&lt;/p>
&lt;p>&lt;code>rlwrap&lt;/code>はGNUの&lt;code>readline&lt;/code>的な振る舞いをするためのパッケージだそうです。&lt;/p>
&lt;h3 id="node側の設定">node側の設定&lt;/h3>
&lt;pre class="lang:sh decode:true " title="~/.bashrc" >alias node="NODE_NO_READLINE=1 rlwrap node"&lt;/pre>
&lt;p>を追加して&lt;code>node&lt;/code>で&lt;code>readline&lt;/code>を使うように設定しておきましょう。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>&lt;a href="https://stackoverflow.com/questions/46714331/how-to-use-reverse-interactive-search-in-nodejs-repl">https://stackoverflow.com/questions/46714331/how-to-use-reverse-interactive-search-in-nodejs-repl&lt;/a>&lt;/p></description></item><item><title>動的に追加したDOM要素にhoverイベントリスナーを追加する</title><link>https://hack.ironsand.net/2014/add-event-listener-to-dynamically-appended-elements/</link><pubDate>Fri, 31 Jan 2014 15:25:08 +0000</pubDate><guid>https://hack.ironsand.net/2014/add-event-listener-to-dynamically-appended-elements/</guid><description>&lt;p>かなりハマったので&lt;code>append()&lt;/code>などを使って動的に追加したHTMLの要素に&lt;code>hover&lt;/code>イベントを追加する方法を残しておきます。説明は全部 coffee script で書いてるので生のJSが良い人は自動変換サービスでもをつかってください。&lt;/p>
&lt;h3 id="前提となる知識">前提となる知識&lt;/h3>
&lt;p>まず基本として &lt;code>$('#foo').click -&amp;gt; console.log &amp;quot;押された！&amp;quot;&lt;/code> のように &lt;code>#foo&lt;/code> に対して click イベントが追加されているわけですが、このイベントの追加はページの読み込み時に行われるために &lt;code>append()&lt;/code> などで後から動的に追加された要素にはイベントの登録がされません。&lt;/p>
&lt;p>なのでそれを解決するために昔の &lt;code>delegate()&lt;/code>, 今なら &lt;code>on()&lt;/code> メソッドがあるわけです。簡単でわかり易い名前に統一されたので見た目はスッキリしたのですが、代わりにサンプルコードを探すのが大変になりました。 &lt;code>on&lt;/code> って単語短すぎやしません…？&lt;/p>
&lt;h3 id="後から追加された要素に-click-イベントを追加">後から追加された要素に click イベントを追加&lt;/h3>
&lt;p>前置きが長くなりましたが&lt;/p>
&lt;pre class="lang:coffee decode:true " >$('#foo').click -&amp;gt; console.log "押された！"&lt;/pre>
&lt;p>を動的に追加された要素に適用させるには&lt;/p>
&lt;pre class="lang:coffee decode:true " >$(document).on 'click', '#foo', -&amp;gt; console.log "押された！"
&lt;/pre>
&lt;p>とします。&lt;code>document&lt;/code> 内部の要素がクリックされるたびにイベントが発火され、クリック対象が &lt;code>#foo&lt;/code> ならメソッドが実行されます。もちろん &lt;code>document&lt;/code> ではなくページ読み込み時に存在する &lt;code>#foo&lt;/code> の親に当たる要素なら何でも構いません。無駄なイベントを発生させないためにできるだけ近い親が良いでしょう。&lt;/p>
&lt;h3 id="後から追加された要素に-hover-イベントを追加">後から追加された要素に hover イベントを追加&lt;/h3>
&lt;p>&lt;del datetime="2014-02-01T01:35:53+00:00">同様に &lt;code>hover&lt;/code> イベントも click イベントのように簡単に書き換えれたらいいのですが、hover イベントを &lt;code>on()&lt;/code> を使って割り当てると &lt;code>hover&lt;/code> は &lt;code>mouseenter&lt;/code> と &lt;code>mouseleave&lt;/code>という複数のイベントを受け取っており、このように複数のイベントを受け取ってる関数を &lt;code>on&lt;/code> メソッドで割り当てるためにはどのイベントを受け取ったのかを判別して処理する必要が出てきます。&lt;/del>&lt;/p>
&lt;p>記述が間違っていたので修正しました。&lt;/p></description></item><item><title>jQueryで要素の存在を確認する `isExist()` と `isNotExist()` を定義する</title><link>https://hack.ironsand.net/2013/check-existance-of-element-with-jquery/</link><pubDate>Mon, 30 Dec 2013 02:45:45 +0000</pubDate><guid>https://hack.ironsand.net/2013/check-existance-of-element-with-jquery/</guid><description>&lt;p>jQuery で選択した要素が存在するかどうかの関数が何故か用意されてないようだったので追加しました。&lt;/p>
&lt;p>CoffeeScriptで書いてるのでjavascriptとして使いたい場合は converter にかけてください。&lt;/p>
&lt;pre class="lang:coffee decode:true " >$.fn.isExist = -&amp;gt;
$(@).length != 0
$.fn.isNotExist = -&amp;gt;
$(@).length == 0 &lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[How to tell if an element exists | jQuery for Designers – Tutorials and screencasts][1]&lt;/p></description></item><item><title>rails の `blank?` メソッドを jQuery に追加する方法</title><link>https://hack.ironsand.net/2013/jquery-isblank-function/</link><pubDate>Sun, 29 Dec 2013 00:34:06 +0000</pubDate><guid>https://hack.ironsand.net/2013/jquery-isblank-function/</guid><description>&lt;p>rails には 値の有無をチェックするときにタブやスペースなどの空白が入っていても「空」だと判断する便利な &lt;code>blank?&lt;/code> 関数が用意されていますが、どうやら JavaScript や jQuery には用意されてないので追加してみました。&lt;/p>
&lt;p>自分で書いた後にもっと良さそうなコードを見つけたのでそちらを紹介します。&lt;/p>
&lt;h3 id="javascript">JavaScript&lt;/h3>
&lt;pre class="lang:js decode:true " title="isBlank.js" >$.fn.isBlank = function() {
return !$(this.html()) || $.trim($(this).html()) === "";
};
&lt;/pre>
&lt;h3 id="coffee">Coffee&lt;/h3>
&lt;p>もちろん Rails 標準のCoffeeも。&lt;/p>
&lt;pre class="lang:coffee decode:true " >$.fn.isBlank = ->
!$(@.html()) || $.trim($(@).html()) is ""&lt;/pre>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[Checking for blank input with jQuery – Stack Overflow][1]&lt;/p>
&lt;p>[jQuery isBlank() | LakTEK (Lakshan Perera)][2]&lt;/p></description></item><item><title>Ajaxで使えるロード画面のgif画像を生成できるサイト</title><link>https://hack.ironsand.net/2013/load-image-for-ajax/</link><pubDate>Tue, 17 Dec 2013 17:18:06 +0000</pubDate><guid>https://hack.ironsand.net/2013/load-image-for-ajax/</guid><description>&lt;p>以前使ったAjax用の load.gif の背景が透過されてなかったので、背景が透過されたの探してたらそういう画像を生成してくれるサイトを見つけました。便利！&lt;/p>
&lt;p>&lt;a href="http://www.ajaxload.info">Ajaxload – Ajax loading gif generator&lt;/a>&lt;/p></description></item><item><title>coffee script を Emacs の smart compile を使ってjsの出力結果を出す</title><link>https://hack.ironsand.net/2013/coffee-script-with-smart-compile/</link><pubDate>Tue, 17 Dec 2013 15:37:33 +0000</pubDate><guid>https://hack.ironsand.net/2013/coffee-script-with-smart-compile/</guid><description>&lt;p>Railsのプロジェクトで coffee script を使うけどもコンパイルは全て Asset pipelineがやってくれるので、たまに変換結果の js をちょっと見てみたい時に困った事になります。&lt;/p>
&lt;p>なのでEmacsの smart compile の機能を使ってすぐに 出力結果を得られるようにしてみました。&lt;/p>
&lt;h3 id="解決策">解決策&lt;/h3>
&lt;p>&lt;code>coffee -p&lt;/code>で標準出力に変換結果を出力してくれるので&lt;/p>
&lt;pre class="lang:lisp decode:true " title="~/.emacs.d/inits/30-smart-compile.el" >(global-set-key (kbd "C-c C-x") 'smart-compile)
(setq smart-compile-alist
(append
'(("\\.rb$" . "ruby %f"))
'(("\\.php$" . "php %f"))
'(("\\.coffee$" . "coffee -p %f"))
'(("\Gemfile$" . "bundle install"))
smart-compile-alist))
&lt;/pre>
&lt;p>としておけばOKです。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>[Convert coffee to javascript and show the result to standard output in Emacs – Stack Overflow][1]&lt;/p></description></item><item><title>JqueryとHTML5でajaxなファイルのアップロード</title><link>https://hack.ironsand.net/2013/ajax-file-upload-by-jquery-and-html5/</link><pubDate>Wed, 27 Feb 2013 09:16:07 +0000</pubDate><guid>https://hack.ironsand.net/2013/ajax-file-upload-by-jquery-and-html5/</guid><description>&lt;p>画面遷移無しでファイルのアップロードしようと思ったらiframeを使った技とかややこしい情報が色々出てきて、なかなかやりたいことに辿りつけなかったのでメモ。&lt;/p>
&lt;h3 id="やりたい事と条件">やりたい事と条件&lt;/h3>
&lt;p>・ajaxなテキストファイルのアップロード&lt;/p>
&lt;p>・テキストをRubyで編集して返す&lt;/p>
&lt;p>・画面遷移せずに結果を表示&lt;/p>
&lt;p>・IEは諦める。&lt;/p>
&lt;h3 id="必要最低限のコード">必要最低限のコード&lt;/h3>
&lt;p>まずHTMLを用意&lt;/p>
&lt;p>&lt;strong>ajaxupload.html&lt;/strong>&lt;/p>
&lt;p>`&lt;/p>&lt;/p>
&lt;pre>&lt;html>&lt;br>
&lt;head>&lt;br>
&lt;meta http-equiv="content-type" content="text/html;charset=utf-8">&lt;br>
&lt;script type="text/javascript" src="jquery-1.7.min.js" charset="utf8">&lt;/script>&lt;br>
&lt;pre>&lt;code>&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;ajaxupload.js&amp;quot; charset=&amp;quot;utf8&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;
&lt;/code>&lt;/pre>
&lt;/head>&lt;br>
&lt;style>textarea{height:80%;width:100%;}&lt;/style>&lt;br>
&lt;body>&lt;br>
&lt;input type=file id=file />&lt;br>
&lt;input type=button id=upload value="upload">&lt;br>&lt;br>
&lt;textarea id=output>&lt;/textarea>&lt;br>
&lt;/body>&lt;br>
&lt;/html>&lt;br>
&lt;/pre>
&lt;p>`
&lt;p>つぎにjavascript&lt;/p>
&lt;p>&lt;strong>ajaxupload.js&lt;/strong>&lt;/p>
&lt;p>`&lt;/p>&lt;/p>
&lt;pre>$(function(){
$("#upload").click(function(){
var file = $("#file")[0].files[0];
var reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = showFile;
});
function showFile(event) {
$.ajax({
type: "POST",
url: "ajaxupload.rb",
data: { filedata:event.target.result}
}).done(function(msg) {
$("#output").html(msg);
});
}
});
&lt;/pre>
&lt;p>`
&lt;p>んで何か処理をするRubyのスクリプト。ここではファイルの中身をそのまま返してるだけ。(パス名は適宜変更してください。)&lt;/p>
&lt;p>&lt;strong>ajaxupload.rb&lt;/strong>&lt;/p>
&lt;p>`&lt;/p>&lt;/p>
&lt;pre>#!P:\Dropbox\bin\RailsPortable\App\Rails\bin\ruby.exe
require "cgi"
cgi = CGI.new
print "Content-Type: text/html\n\n"
print cgi['filedata'][0]&lt;/pre>
&lt;p>`
&lt;h3 id="デモ">デモ&lt;/h3>
&lt;p>サンプルを置いておきます。(さくらのレンタルサーバーはRubyの拡張子がrbではなくcgiじゃなくと動かなかったのでファイル名を変えています。&lt;/p></description></item><item><title>javascript で大文字、小文字を無視して文字列比較</title><link>https://hack.ironsand.net/2012/javascript-compare-strings-ignoring-case/</link><pubDate>Sat, 07 Jan 2012 21:12:25 +0000</pubDate><guid>https://hack.ironsand.net/2012/javascript-compare-strings-ignoring-case/</guid><description>&lt;p>javascript で大文字、小文字を区別しないで文字列比較がしたい時は、そういう演算子は用意されてないので toLowerCase() で全部小文字にして比較しちゃいましょう。&lt;/p>
&lt;p>こんな感じ。&lt;/p>
&lt;pre>function check(){
foo="ABC";
bar="abc";
if(foo.toLowerCase() == bar.toLowerCase()){ //大文字小文字を無視
alert("ok!!");
}else{
alert("NG!");
}
}&lt;/pre>
&lt;p>ちなみに英語で「大文字、小文字を区別しない。」と言いたい時は case-insensitive を使えばよいそうですよ。&lt;/p></description></item><item><title>jQueryでマウスの右クリック、左クリックを判断する</title><link>https://hack.ironsand.net/2012/how-to-obtain-the-clicked-mouse-button-by-jquery/</link><pubDate>Sat, 07 Jan 2012 03:41:05 +0000</pubDate><guid>https://hack.ironsand.net/2012/how-to-obtain-the-clicked-mouse-button-by-jquery/</guid><description>&lt;p>jQuery を使って&lt;/p>
&lt;p>「クリックされた時に左なら要素を消して、右なら何もしない。」&lt;/p>
&lt;p>という動作をさせようと思ったら日本語だと検索してもすぐに答えが出てこなかったので、また stackoverflow から解説を紹介します。&lt;/p>
&lt;p>実際のコードがこちら。&lt;/p>
&lt;pre>$('#element').mousedown(function(event) {
switch (event.which) {
case 1:
alert('Left mouse button pressed');
break;
case 2:
alert('Middle mouse button pressed');
break;
case 3:
alert('Right mouse button pressed');
break;
default:
alert('You have a strange mouse');
}
});
&lt;/pre>
&lt;p>mousedown の時に渡されるイベントを確認して場合わけすればよい、と。&lt;/p>
&lt;p>つまり左クリックで要素を隠すにはこうします。&lt;/p>
&lt;pre>//左クリックされると foo を隠す
$(".foo").click(function(e){
if(e.which == 1){ //左クリック
$(this).hide("fast");
}
});&lt;/pre>
&lt;h3 id="他の方法">他の方法&lt;/h3>
&lt;p>頻繁に使うのであれば jquery.detailclick.js を入れてこんな風に使えるようにしておくとよいかもしれません。やっぱりこちらの方が jQuery っぽくて見やすいですね。&lt;/p>
&lt;pre>$("target").rightClick(function()&lt;br />
{&lt;br />
$(this).text('右クリックされました！').css('background-color', '#ff3399');&lt;br />
});&lt;/p>
&lt;pre>
&lt;h3>
参考
&lt;/h3>
&lt;p>
javascript - How to distinguish between left and right mouse click with jQuery? - Stack Overflow&lt;br />
&lt;a href="http://stackoverflow.com/questions/1206203/how-to-distinguish-between-left-and-right-mouse-click-with-jquery">http://stackoverflow.com/questions/1206203/how-to-distinguish-between-left-and-right-mouse-click-with-jquery&lt;/a>&lt;br />
JSNote - 左、中、右クリックのカスタムイベントを追加するjQueryプラグイン&lt;br />
&lt;a href="http://jsnt.blog.fc2.com/blog-entry-3.html">http://jsnt.blog.fc2.com/blog-entry-3.html&lt;/a>
&lt;/p></description></item><item><title>$(window).width() と $(document).width() 違い</title><link>https://hack.ironsand.net/2012/difference-between-window-width-and-document-width/</link><pubDate>Sat, 07 Jan 2012 03:25:58 +0000</pubDate><guid>https://hack.ironsand.net/2012/difference-between-window-width-and-document-width/</guid><description>&lt;p>$(window).width() と $(document).width() や&lt;/p>
&lt;p>$(window).height() と $(document).height() がどういう時に違う値を返すかわからなかったので検索したら Stack Overflow に簡潔でわかりやすい説明がされてたので紹介します。&lt;/p>
&lt;blockquote>
&lt;p>When you have a scrollbar on the webpage.&lt;/p>&lt;/blockquote>
&lt;blockquote>
&lt;p>(スクロールバーが表示されてる時)&lt;/p>&lt;/blockquote>
&lt;p>わかりやすいですね。一応念のために書いておくと window が「ブラウザの表示領域」で、document が「ページ全体」です。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>jquery – When can $(document).width() and $(window).width() show different values? – Stack Overflow&lt;/p>
&lt;p>&lt;a href="http://stackoverflow.com/questions/8134741/when-can-document-width-and-window-width-show-different-values">http://stackoverflow.com/questions/8134741/when-can-document-width-and-window-width-show-different-values&lt;/a>&lt;/p></description></item><item><title>jQueryの便利なmapの使い方</title><link>https://hack.ironsand.net/2012/how-to-use-map-in-jquery/</link><pubDate>Sun, 01 Jan 2012 02:00:09 +0000</pubDate><guid>https://hack.ironsand.net/2012/how-to-use-map-in-jquery/</guid><description>&lt;p>lispでmapに初めて触れた時は「何、このめんどくさい関数？」と感じ、正直な話あまり触れたくもなかったのですが慣れてくるとこいつはとても便利な賢いやつです。&lt;/p>
&lt;p>さてjavascriptでもmapが使えるか調べてみたらjQueryで提供されてました。&lt;/p>
&lt;p>mapを使えば配列全部に関数を適用させて、返り値を配列で受け取れます。for文なんて使わなくてもええんやっ！！&lt;/p>
&lt;h3 id="使い方の例">使い方の例&lt;/h3>
&lt;p>定番の累乗を求めるサンプル。&lt;/p>
&lt;pre>arr = [1,2,3];
arr = $.map(arr, function(n,i){return n*n;});
for(i=0;i&amp;lt;arr.length;i++){
alert(arr[i]);
}&lt;/pre>
&lt;p>サンプルには良く使われるけど、実際にプログラミングしてて累乗が必要になったことがないですね。まあサンプルなんで実用性よりも分かりやすさが重要だから当然といえば当然の話しですね。&lt;/p>
&lt;p>とりあえずこんな感じの動作がmapはできます。&lt;/p>
&lt;h3 id="要素をまとめて取得">要素をまとめて取得&lt;/h3>
&lt;p>たとえばこんなhtmlがあって&lt;/p>
&lt;pre>&amp;lt;div&amp;gt;1&amp;lt;/div&amp;gt;
&amp;lt;div&amp;gt;2&amp;lt;/div&amp;gt;
&amp;lt;div&amp;gt;3&amp;lt;/div&amp;gt;&lt;/pre>
&lt;p>配列で[1,2,3]が欲しいとする。&lt;/p>
&lt;p>&lt;strong>そんな時もこの map を使えば一発解決さ！&lt;/strong>(深夜の外人がやってる通販のノリで)&lt;/p>
&lt;pre>arr = $("div").map(function(){
return $(this).text();
}).get();&lt;/pre>
&lt;p>これでとれる。注意点は最後の get()。これがなくても配列っぽいのが返ってくるんだけども、これはjQuery-wrapped arrayというものらしく別物とのこと。&lt;/p>
&lt;p>for文で実行したり arr[0] で要素のアクセスしたりはできるけどもjoinすると上手く行かない。このjoinがうまくできないのにはまってだいぶ時間を食われました。&lt;/p>
&lt;p>arr.&lt;strong>constructor&lt;/strong> でクラスを調べたら&lt;/p>
&lt;pre>function Array() {
[native code]
}&lt;/pre>
&lt;p>となるべき所が&lt;/p>
&lt;pre>function (a, b) {
return new e.fn.init(a, b, h);
}&lt;/pre>
&lt;p>になってました。この「get() すればよい」という情報を得るためにどれだけ遠回りしたことか…。&lt;/p>
&lt;p>とはいえこれで安心してmapをjavascriptでも使えるようになったのでガシガシ使っていこうと思います。&lt;/p>
&lt;h3 id="参考">参考&lt;/h3>
&lt;p>&lt;a href="http://api.jquery.com/map/" target="_blank">&lt;a href="http://api.jquery.com/map/">http://api.jquery.com/map/&lt;/a>&lt;/a>&lt;/p></description></item><item><title>Windows(cygwin+zsh)環境でRhinoを使う</title><link>https://hack.ironsand.net/2010/use-rhino-in-windows-cygwin-zsh/</link><pubDate>Sun, 14 Mar 2010 02:16:35 +0000</pubDate><guid>https://hack.ironsand.net/2010/use-rhino-in-windows-cygwin-zsh/</guid><description>&lt;h4 id="rhinoとは何か">Rhinoとは何か？&lt;/h4>
&lt;p>javascript を記述するときに毎回htmlファイルを作る作業が手間なので何か方法がないか探してみた。 どうやら、JAVAで書かれたjavascript実装のRhinoでやりたい事ができるらしい。&lt;/p>
&lt;blockquote cite="https://developer.mozilla.org/ja/Rhino">
&lt;p>
&lt;a href="https://developer.mozilla.org/ja/Rhino">Rhino &amp;#8211; MDC&lt;/a>
&lt;/p>
&lt;p>
Rhino はすべてが Java で記述された JavaScript のオープンソースな実装です。それは一般的には、Java アプリケーション環境へ組み込まれて、エンドユーザーによるスクリプトの記述が可能になります。
&lt;/p>
&lt;/blockquote>
&lt;h4 id="rhinoのインストール方法">Rhinoのインストール方法&lt;/h4>
&lt;p>J2SE 6には標準で入っているらしいがEverythingで探しても見つからないので今回は &lt;a href="http://www.mozilla-japan.org/rhino/download.html">http://www.mozilla-japan.org/rhino/download.html&lt;/a> からダウンロードして使うことにする。&lt;/p>
&lt;p>ダウンロード+解凍して適当なディレクトリに移動する(私の環境ではSugarSyncで共有してるディレクトリ配下のX:\sugarsync\bin\rhino1_7R2)。 スペースや日本語の文字列を含まない場所に置くのが無難だろう。 次に CLASSPATH を設定する。&lt;pre class=prettyprint>CLASSPATH=x:\sygarsync\bin\rhino1_7R2\js.jar export CLASSPATH # /cygdrive/x/～ の形式では java が認識しない。&lt;/pre>&lt;/p>
&lt;p>これでインストールは完了したので&lt;/p>
&lt;pre>java org.mozilla.javascript.tools.shell.Main&lt;/pre>
&lt;p>と入力すればインタープリタが起動する。&lt;/p>
&lt;h4 id="zshrcでエイリアスの設定">zshrcでエイリアスの設定&lt;/h4>
&lt;p>しかし毎回これを打つのはあまり現実的ではないため ~/zshrc で alias を設定しておくと良い。&lt;pre class=prettyprint>alias js=&amp;ldquo;java org.mozilla.javascript.tools.shell.Main&amp;rdquo;&lt;/pre>&lt;/p>
&lt;p>インタープリタを呼び出さずに直接実行することや、jsファイルを直接指定して実行することもできる。&lt;pre class=prettyprint>js -e &amp;ldquo;print(&amp;lsquo;hello&amp;rsquo;)&amp;rdquo; js -f hello.js&lt;/pre>&lt;/p>
&lt;p>これでjavascriptの動作をちょっと確認したいときに簡単に動かせるようになったし、JAVAで実装されているため、JAVAのクラスをjavascript内部で呼び出したりもできるようだ。 色々と遊んでいこうと思う。&lt;/p></description></item><item><title>xyzzy から Rhino(javascript)を直接実行する</title><link>https://hack.ironsand.net/2010/xyzzy%E3%81%8B%E3%82%89rhino-javascript%E3%82%92%E7%9B%B4%E6%8E%A5%E5%AE%9F%E8%A1%8C%E3%81%99%E3%82%8B/</link><pubDate>Sun, 14 Mar 2010 00:00:00 +0000</pubDate><guid>https://hack.ironsand.net/2010/xyzzy%E3%81%8B%E3%82%89rhino-javascript%E3%82%92%E7%9B%B4%E6%8E%A5%E5%AE%9F%E8%A1%8C%E3%81%99%E3%82%8B/</guid><description>&lt;h4 id="xyzzyから直接javascriptを使う">xyzzyから直接javascriptを使う&lt;/h4>
&lt;p>今回の記事はRhinoのインストールが行われてる事と xyzzy で jscript-mode がインストールされている事が前提条件になります。 RhinoのWindowsでのインストール方法については前回の記事『[Windows(cygwin+zsh)環境でRhinoを使う][1]』を参考にしてください。 今回の記事の設定を行うと xyzzy から直接Rhinoを呼び出して下の画像のように簡単なスクリプトを気軽に書けるようになります。&lt;/p>
&lt;p>下記のlispはruby-modeから引っ張ってきて少し修正を加えたもので、これを ~/.xyzzy や siteinitl. に貼りつければjscript-mode で C-c C-x すれば現在編集中のバッファの javascript が実行されます。&lt;/p>
&lt;pre class=prettyprint>;;jscript-mode(for Rhino) (setf \*js-prog\* "java org.mozilla.javascript.tools.shell.Main") (setf \*js-classpath\* "X:/sugarsync/bin/rhino1\_7R2/js.jar") (defun js-run-script-immediate () (interactive) (js-run "")) (defun js-run (args) (let ((tempfile (make-temp-file-name "\_\_temp\_" "js" (default-directory)))) (write-file tempfile t nil (buffer-fileio-encoding) (buffer-eol-code)) (command-execution (concat \*js-prog\* " -f \"" tempfile "\" " args)) (delete-file tempfile :if-does-not-exist :skip))) (defun command-execution (command) (interactive "sCmmand:") (let ((proc nil)(buffer (selected-buffer))) (with-output-to-temp-buffer ("\*cmd\*" 5) (unwind-protect (setq proc (make-process command :environ \`(("CLASSPATH" . ,\*js-classpath\*)) :output(selected-buffer) :exec-directory (default-directory buffer))))) (while (eq :run (process-status proc)) (sit-for 0.05)(do-events)) (if (= 0 (point-max)) (let ((buff (selected-buffer))) (other-window) (delete-buffer buff) (delete-other-windows)) (other-window)) (message (concat " '" command "' ended.")))) (add-hook 'ed::\*jscript-mode-hook\* #'(lambda () (define-key ed::\*jscript-mode-map\* '(#\C-c #\C-x) 'js-run-script-immediate)))&lt;/pre>
&lt;p>*js-classpath* と define-key は好みに合わせて適宜変更の事。&lt;/p></description></item><item><title>MS、Webブラウザ選択画面のバグの解説</title><link>https://hack.ironsand.net/2010/microsoft-explains-bug-about-browser-select/</link><pubDate>Sat, 13 Mar 2010 17:58:35 +0000</pubDate><guid>https://hack.ironsand.net/2010/microsoft-explains-bug-about-browser-select/</guid><description>&lt;p>MS のブラウザ選択のバグに関する説明が、全くわかってない人が書いてるとしか思えない内容だったので javascript の sort について少し解説をしようと思う。&lt;/p>
&lt;blockquote cite="http://www.atmarkit.co.jp/news/201003/01/ballot.html">
&lt;p>
&lt;a href="http://www.atmarkit.co.jp/news/201003/01/ballot.html">MS、Webブラウザ選択画面にアルゴリズム上のバグか － ＠IT&lt;/a>
&lt;/p>
&lt;p>
JavaScriptの配列の並べ替え（sort）は比較関数を渡すことで柔軟な並べ替えが行える。ここではMath.rand()で半々の確率で正負の値を返しているので、隣り合う要素の並べ替えはランダムに起こる。これで結果もランダムになるように思えるが、実際に意味のある並べ替えを行うためには、並べ替えた後の配列が一定の基準に従っている並べられていることが不可欠で、例えば「a＞b」であれば「b＜a」、「a＜bかつb＜c」であれば「a＜c」でなければならない。これは、隣り合う要素同士をただランダムに入れ替えるだけでは達成できないのだという。
&lt;/p>
&lt;/blockquote>
&lt;p>この「並べ替えた後の配列が一定の基準に従っている並べられていることが不可欠」という意味の分からない部分はおそらく下記を訳した物と思われる。&lt;/p>
&lt;blockquote cite="http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html">
&lt;p>
&lt;a href="http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html">Doing the Microsoft Shuffle: Algorithm Fail in Browser Ballot&lt;/a>
&lt;/p>
&lt;p>
Sorting requires a self-consistent definition of ordering. The following assertions must be true if sorting is to make any sense at all:
&lt;/p>
&lt;/blockquote>
&lt;p>だから正しい訳は「並べかえには首尾一貫した&amp;quot;大小の定義&amp;quot;が必要である。」になる。&lt;/p>
&lt;p>実際にどのようなバグがあったかを説明するにはコードで示すのが一番わかりやすいので直接書いていこう。 問題の、 javascript の sort 関数では引数に比較関数を取れる。例えば&lt;pre class=prettyprint>function comp(a, b){ return a &amp;lt; b; //降順で並べる } function sortnum(){ return [0,3,5,6,-6].sort(comp); } //=&amp;gt;6,5,3,0,-6&lt;/pre> &lt;pre class=prettyprint>function comp(a, b){ return a &amp;lt; b; //昇順で並べる } function sortnum(){ return [0,3,5,6,-6].sort(comp); } //=&amp;gt;-6,0,3,5,6&lt;/pre>&lt;/p></description></item></channel></rss>