履歴: プログラミング/CoffeeScript
各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 18 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます
| 日時 | タイトル | 文字数 | 追加 | 削除 | 内容 | 差分 | 基準 |
|---|---|---|---|---|---|---|---|
| 2016/4/8 18:12:11 | プログラミング/CoffeeScript 最新版 | 30,938 | +1 | -22 | 表示 | 表示中 | — |
| 2016/4/8 18:12:11 | プログラミング/CoffeeScript | 31,782 | +1 | -1 | 表示 | 差分 | 選択 |
| 2016/4/8 9:12:11 | プログラミング/CoffeeScript | 31,761 | +10 | -4 | 表示 | 差分 | 選択 |
| 2016/3/9 4:20:09 | プログラミング/CoffeeScript | 31,740 | +8 | -1 | 表示 | 差分 | 選択 |
| 2014/5/29 16:05:27 | プログラミング/CoffeeScript | 31,570 | +62 | -15 | 表示 | 差分 | 選択 |
| 2014/5/29 9:52:19 | プログラミング/CoffeeScript | 29,354 | +150 | -27 | 表示 | 差分 | 選択 |
| 2014/5/28 15:58:53 | プログラミング/CoffeeScript | 24,913 | +191 | -2 | 表示 | 差分 | 選択 |
| 2013/11/27 15:06:40 | プログラミング/CoffeeScript | 18,653 | +3 | -0 | 表示 | 差分 | 選択 |
| 2013/11/23 13:44:28 | プログラミング/CoffeeScript | 18,096 | +18 | -0 | 表示 | 差分 | 選択 |
| 2013/5/9 5:34:07 | プログラミング/CoffeeScript | 17,815 | +1 | -1 | 表示 | 差分 | 選択 |
| 2013/5/9 5:34:07 | プログラミング/CoffeeScript | 17,809 | +6 | -5 | 表示 | 差分 | 選択 |
| 2013/5/9 5:34:07 | プログラミング/CoffeeScript | 17,746 | +8 | -0 | 表示 | 差分 | 選択 |
| 2013/5/2 10:58:05 | プログラミング/CoffeeScript | 17,595 | +44 | -0 | 表示 | 差分 | 選択 |
| 2013/4/18 17:07:42 | プログラミング/CoffeeScript | 16,718 | +17 | -17 | 表示 | 差分 | 基準 |
| 2013/4/18 17:07:42 | プログラミング/CoffeeScript | 16,697 | +56 | -5 | 表示 | 差分 | 選択 |
| 2013/4/17 19:06:55 | プログラミング/CoffeeScript | 15,816 | +7 | -3 | 表示 | 差分 | 選択 |
| 2013/4/17 18:54:18 | プログラミング/CoffeeScript | 15,740 | +381 | -1 | 表示 | 差分 | 選択 |
| 2013/4/17 12:53:40 | プログラミング/CoffeeScript | 626 | — | — | 表示 | — | 選択 |
基準: プログラミング/CoffeeScript (2013/4/18 17:07:42) → 比較: プログラミング/CoffeeScript (2016/4/8 18:12:11) 最新版
@@ -8,8 +8,8 @@
もっと簡単に JavaScript を書きたい、という人向けに作られた言語だそうです。
-Play Framework には自動的に JavaScript に変換してくれる機構が備わっているので
-シームレスに簡単に使うことができます
+最近の Ruby on Rails や [[Play Framework>ソフトウェア/PlayFramework/チュートリアル+α]] には自動的に JavaScript に変換してくれる機構が備わっているので
+シームレスに使うことができます
** メリット [#sd53cdfc]
@@ -41,6 +41,7 @@
- 普通の JavaScript が書きたくなくなる
- 普通の for( ; ; ) が書けないのが痛いときがある
+- 最近はあまりはやっていないみたい???
*** for ... in ... と for ... of ... の見分けが付きにくい [#ae60a738]
@@ -63,13 +64,57 @@
まあ、エラーが出たら => に直すというスタンスで書いておけば、
読むときは気にせず読めるから良いのかもしれないけれど???
+*** 多重ループから脱出するためのラベル付き continue, break がない [#l67c096e]
+
+https://groups.google.com/forum/#!msg/coffeescript/-LJ5OB2FNUw/UVrRdFwWdh8J
+
+このあたりを読む限り、
+CoffeeScript では JavaScript のラベルを実装する予定は無いみたいですね。
+
+それじゃどうするかというと、~
+http://stackoverflow.com/questions/7655786/breaking-continuing-nested-for-loops-in-coffeescript ~
+で議論されているように、
+
+ LANG:coffeescript
+ list = ["a", "b"]
+
+ `outer://`
+ for i in list
+ for j in [1..2]
+ alert(i + j)
+ `continue outer`
+
+バッククオートでエスケープしたラベルや continue を渡してやるのが良いみたいです。
+
+~`outer:` ではなく `outer://` となっている理由は、
+上記コードを JavaScript に変換して
+
+ LANG:javascript
+ var i, j, list, _i, _j, _len;
+
+ list = ["a", "b"];
+
+ outer://;
+
+ for (_i = 0, _len = list.length; _i < _len; _i++) {
+ i = list[_i];
+ for (j = _j = 1; _j <= 2; j = ++_j) {
+ alert(i + j);
+ continue outer;
+ }
+ }
+
+となるのを見ると明らかだと思います。
+
+outer: の後ろに ; が入るので、それをコメントアウトしているわけですね。
+
* 書いてみた [#wbd461e8]
-** 日本の休日を求めるスクリプト [#p3b41c31]
+** 日本の休日を求めるスクリプト(休日判定) [#p3b41c31]
まあ、車輪の再発明っぽいところもあるわけですが、
祝日名が取れて、なおかつメンテナンスしやすい形の
-ライブラリが見付からなかったので。
+ライブラリが見付からなかったので書いてみました。
一応、https://gist.github.com/Songmu/703311 と突き合わせて
齟齬がないことを確認しましたが、運用は個々の責任でお願いします。
@@ -77,8 +122,20 @@
法律改正などで休日が変更になっても、よほどのことがない限り definition
のところをいじるだけで対応できるはず???
+*** 最新版はこちらです [#j372e199]
+
+現在はこちらで公開しています。
+
+[[ソフトウェア/javascript/japanese-holidays]]
+
*** ソース [#yac07e9e]
+COLOR(red){SIZE(40){''以下のコードは古いです''}}
+
+最新版はこちらで公開しています。
+
+[[ソフトウェア/javascript/japanese-holidays]]
+
LANG:coffeescript(linenumber)
###
@@ -111,11 +168,11 @@
**** Date.getHolidaysOf(year, furikae = true)
- 指定された年の休日を連想配列にして返す
- 連想配列は "month,day" -> name_of_holiday の形になる
+ 指定された年の休日を配列にして返す
+ 配列には {month:m, date:d, name:s} の形で表わされた休日が日付順に並ぶ
furikae に false を指定すると、振替休日および国民の休日を除く
- 例(JavaScript):
+ JavaScript:
today = new Date();
holidays = Date.getHolidaysOf( today.getFullYear() );
@@ -343,6 +400,14 @@
1;d=holiday.getDate();w_furikae[[m,d]]="振替休日"}}holidays[true][y]=w_furikae;return holidays[furikae][y]};Date.getHolidaysOf=function(y,furikae){var month_day;var name;var result;var _ref;result=[];_ref=getHolidaysOf(y,furikae);for(month_day in _ref){name=_ref[month_day];result.push({month:parseInt(month_day.split(",")[0]),date:parseInt(month_day.split(",")[1]),name:name})}result.sort(function(a,b){return a.month-b.month||a.date-b.date});return result};Date.prototype.isHoliday=function(furikae){return getHolidaysOf(this.getFullYear(),
furikae)[[this.getMonth()+1,this.getDate()]]}}).call(this);
+コメントやスペースをすべて除いたタイプ量を比べると
+|Coffee Script| 3650 文字|
+|Java Script|4751 文字|
+|比率|0.768|
+
+となって、この単純な例では Coffee Script を使うことによって
+タイプ量が 3/4 程度で済んでいることになります。
+
*** これを使ってカレンダーを表示 [#c33aff1a]
&attachref(calendar.png);
@@ -390,7 +455,8 @@
d = d.getShifted(0,0,1)
return calendar
-土曜日と日曜日は css の nth-child で色を付けられる。
+土曜日と日曜日は css の nth-child で色を付ければ Script
+側で特別処理をしておく必要はありません。
LANG:css
div.calendar {
@@ -465,6 +531,365 @@
return elem
return null
+** Object.is_a, Object::is_a [#l2369fea]
+
+javascript の instanceof 演算子が壊れた仕様であることは有名ですが、
+
+参照: http://minghai.github.io/library/coffeescript/07_the_bad_parts.html~
+ の typeof の項
+
+調べたいオブジェクトを Object() で包んでから渡してやりさえすれば、
+ほぼ期待通りの動作をしてくれるのだそうです。
+
+参照: http://webreflection.blogspot.jp/2011/10/better-isa-function-for-js.html
+
+で、is_a という関数を定義する際、
+obj.is_a(type) と書けるようにすればタイプ量が少なくて一見良さそうなのですが、
+null や undefined に対して呼べなくなってしまうため、
+javascript から使うには使いにくくなってしまいます。
+そこで、javascript 用には Object.is_a(obj, type) という仕様の関数を用意しました。
+
+一方 Coffee Script では ? を挟んで obj?.is_a(type) と呼べば null や undefined にも対応できるので、
+Object::is_a(type) も便利に使えます。さらに、obj.is_a(type) 形式で呼び出す場合には、
+is_a の呼び出し時に数値や文字列などのネイティブオブジェクトが Object にカプセル化されるため、
+is_a 関数内でもう一度カプセル化してやる必要はないようです。
+
+ただ、異なるフレームで作成されたオブジェクトに対してこの関数を呼んでも正しく判定できないようです。~
+参照:http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
+
+*** ソース [#hbe8e397]
+
+ LANG:coffeescript
+ Object::is_a = (type)->
+ this instanceof type
+
+ Object.is_a = (obj, type)->
+ obj? and Object(obj) instanceof type
+
+*** テスト [#p49d26a4]
+
+ LANG:coffeescript
+ # for jasmine
+ describe "Object.is_a", ->
+
+ it "distinguishes Number", ->
+ expect( Object.is_a(1, Number) ).toBe true
+ expect( Object.is_a(1, Object) ).toBe true
+ expect( Object.is_a(new Number(1), Number) ).toBe true
+
+ it "distinguishes String", ->
+ expect( Object.is_a("a", String) ).toBe true
+ expect( Object.is_a("a", Object) ).toBe true
+ expect( Object.is_a(new String("a"), String) ).toBe true
+
+ it "distinguishes RegExp", ->
+ expect( Object.is_a(/a/, RegExp) ).toBe true
+ expect( Object.is_a(/a/, Object) ).toBe true
+ expect( Object.is_a(new RegExp("a"), RegExp) ).toBe true
+
+ it "distinguishes Boolean", ->
+ expect( Object.is_a(true, Boolean) ).toBe true
+ expect( Object.is_a(true, Object) ).toBe true
+ expect( Object.is_a(1==1, Boolean) ).toBe true
+ expect( Object.is_a(new Boolean(true), Boolean) ).toBe true
+
+ it "distinguishes Array", ->
+ expect( Object.is_a([], Array) ).toBe true
+ expect( Object.is_a([], Object) ).toBe true
+ expect( Object.is_a(new Array([]), Array) ).toBe true
+
+ it "distinguishes Object", ->
+ expect( Object.is_a({}, Object) ).toBe true
+
+ it "answers false for null", ->
+ expect( Object.is_a(null, Object) ).toBe false
+
+ it "answers false for undefined", ->
+ expect( Object.is_a(undefined, Object) ).toBe false
+
+ it "distinguishes with ancestors", ->
+ class A
+ class B extends A
+ class C extends B
+ expect( Object.is_a(new C, Object) ).toBe true
+ expect( Object.is_a(new C, A) ).toBe true
+ expect( Object.is_a(new C, B) ).toBe true
+ expect( Object.is_a(new C, C) ).toBe true
+ expect( Object.is_a(new B, Object) ).toBe true
+ expect( Object.is_a(new B, A) ).toBe true
+ expect( Object.is_a(new B, B) ).toBe true
+ expect( Object.is_a(new B, C) ).toBe false
+ expect( Object.is_a(new A, Object) ).toBe true
+ expect( Object.is_a(new A, A) ).toBe true
+ expect( Object.is_a(new A, B) ).toBe false
+ expect( Object.is_a(new A, C) ).toBe false
+
+ it "distinguishes objects with constructor of object", ->
+ class A
+ class B extends A
+ o = {}
+ a = new A
+ b = new B
+
+ expect( o.constructor ).toBe Object
+ expect( a.constructor ).toBe A
+ expect( b.constructor ).toBe B
+ expect( o.constructor.__super__ ).toBeUndefined()
+ expect( a.constructor.__super__ ).toBeUndefined()
+ expect( b.constructor.__super__.constructor ).toBe A
+
+ expect( Object.is_a(o, o.constructor) ).toBe true
+ expect( Object.is_a(o, a.constructor) ).toBe false
+ expect( Object.is_a(o, b.constructor) ).toBe false
+ expect( Object.is_a(a, o.constructor) ).toBe true
+ expect( Object.is_a(a, a.constructor) ).toBe true
+ expect( Object.is_a(a, b.constructor) ).toBe false
+ expect( Object.is_a(b, o.constructor) ).toBe true
+ expect( Object.is_a(b, a.constructor) ).toBe true
+ expect( Object.is_a(b, b.constructor) ).toBe true
+
+
+ describe "Object::is_a", ->
+
+ it "works with undefined", ->
+ expect( undefined?.is_a(Object) ).toBeFalsy()
+ expect( undefined_obj?.is_a(Object) ).toBeFalsy()
+ `var uninitialized_obj`
+ expect( uninitialized_obj?.is_a(Object) ).toBeFalsy()
+
+ it "works with null", ->
+ expect( null?.is_a(Object) ).toBeFalsy()
+
+ it "works with literal objects", ->
+ expect( 0 instanceof Number ).toBeFalsy()
+ expect( NaN instanceof Number ).toBeFalsy()
+ expect( 0?.is_a(Number) ).toBeTruthy()
+ expect( NaN?.is_a(Number) ).toBeTruthy()
+ expect( "a" instanceof String ).toBeFalsy()
+ expect( "a"?.is_a(String) ).toBeTruthy()
+ expect( /a/ instanceof RegExp ).toBeTruthy()
+ expect( /a/?.is_a(RegExp) ).toBeTruthy()
+ expect( false instanceof Boolean ).toBeFalsy()
+ expect( (0==0) instanceof Boolean).toBeFalsy()
+ expect( false?.is_a(Boolean) ).toBeTruthy()
+ expect( (0==0)?.is_a(Boolean) ).toBeTruthy()
+ expect( [] instanceof Array ).toBeTruthy()
+ expect( []?.is_a(Array) ).toBeTruthy()
+ expect( {} instanceof Object ).toBeTruthy()
+ expect( {}?.is_a(Object) ).toBeTruthy()
+
+ it "works with the super class", ->
+ class A
+ class B extends A
+ class C extends B
+ a = new A
+ b = new B
+ c = new C
+ expect( c?.is_a(Object) ).toBeTruthy()
+ expect( c?.is_a(A) ).toBeTruthy()
+ expect( c?.is_a(B) ).toBeTruthy()
+ expect( c?.is_a(C) ).toBeTruthy()
+ expect( b?.is_a(Object) ).toBeTruthy()
+ expect( b?.is_a(A) ).toBeTruthy()
+ expect( b?.is_a(B) ).toBeTruthy()
+ expect( b?.is_a(C) ).toBeFalsy()
+ expect( a?.is_a(Object) ).toBeTruthy()
+ expect( a?.is_a(A) ).toBeTruthy()
+ expect( a?.is_a(B) ).toBeFalsy()
+ expect( a?.is_a(C) ).toBeFalsy()
+
+
+** Object.class_name_of [#e877d780]
+
+こちらも null や undefined に対応するため、Object.class_name_of(obj) の形になっています。
+
+http://webreflection.blogspot.jp/2007/01/javascript-getclass-and-isa-functions.html
+のコードはたぶん意図と異なるコードになっているので、気持ちを汲み取って書き直してみました。
+
+http://api.jquery.com/jQuery.type/ ~
+http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
+
+このあたりを見ると Function.prototype.toString() は実装依存なため下記コードは
+必ずしもすべてのブラウザで動かない可能性もあるのですが、とりあえず試した範囲では大丈夫そう、
+という感じです。
+
+*** ソース [#rabfc8d7]
+
+ LANG:coffeescript
+ root = exports ? this
+
+ Object.class_name_of = (obj)->
+ return "null" if obj == null
+ return "undefined" if obj == undefined
+ if obj.constructor.toString().match /function\s+(\S+)\s*\(/
+ return RegExp.$1
+ if obj.constructor.prototype?
+ # search constructor in the global scope
+ for key, value of root
+ if value?.prototype? and
+ obj instanceof value
+ return key
+ return "anonymous"
+
+*** 単純版のソース [#m8135b57]
+
+上記のコードがややこしい理由は、
+
+ LANG:javascript
+ function MyClass(){};
+ obj = new MyClass();
+ alert(Object.class_name_of(obj));
+
+ではなく、
+
+ LANG:javascript
+ MyClass = function(){}; // グローバルに宣言された変数
+ obj = new MyClass();
+ alert(Object.class_name_of(obj));
+
+とした場合にも名前を判別できるよう工夫しているためです。
+(たぶん、リンク先ではそのあたりのコードがおかしくなっているように思えるのですが・・・)
+
+その際、グローバルオブジェクトのプロパティをスキャンしているため、
+この関数を無名クラスのオブジェクトに対して頻繁に呼び出すと、
+パフォーマンス的に問題が生じる可能性があります。
+
+Coffee Script を使っている限り
+上記のような無名クラスのオブジェクトを作成することはほとんどありませんし、
+そもそも上のように頑張ったところで、
+
+ LANG:javascript
+ var MyClass = function(){}; // ローカルに宣言された変数
+ obj = new MyClass();
+ alert(Object.class_name_of(obj));
+
+こういう場合にはうまく行きません。
+
+そこで無名関数から作成されたオブジェクトに対して "anonymous" を返して構わないことにすれば、
+if obj.constructor.prototype? の部分を除いた次のコードで良いことになります。
+
+ LANG:coffeescript
+ Object.class_name_of = (obj)->
+ return "null" if obj == null
+ return "undefined" if obj == undefined
+ if obj.constructor.toString().match /function\s+(\S+)\(/
+ return RegExp.$1
+ return "anonymous"
+
+*** テスト [#v74d0bf8]
+
+ LANG:coffeescript
+ # for jasmine
+ root = exports ? this
+
+ describe "Object.class_name_of", ->
+
+ it "distinguishes null", ->
+ expect( Object.class_name_of(null) ).toBe "null"
+
+ it "distinguishes undefined", ->
+ expect( Object.class_name_of(undefined) ).toBe "undefined"
+ a = {}
+ expect( Object.class_name_of(a["abc"]) ).toBe "undefined"
+
+ it "distinguishes Object", ->
+ expect( Object.class_name_of({}) ).toBe "Object"
+ expect( Object.class_name_of(new Object()) ).toBe "Object"
+
+ it "distinguishes Number", ->
+ expect( Object.class_name_of(1) ).toBe "Number"
+ expect( Object.class_name_of(new Object(1)) ).toBe "Number"
+ expect( Object.class_name_of(Number(1)) ).toBe "Number"
+ expect( Object.class_name_of(new Number(1)) ).toBe "Number"
+
+ it "distinguishes Array", ->
+ expect( Object.class_name_of([]) ).toBe "Array"
+ expect( Object.class_name_of(new Object([])) ).toBe "Array"
+ expect( Object.class_name_of(new Array()) ).toBe "Array"
+
+ it "distinguishes RegExp", ->
+ expect( Object.class_name_of(/a/) ).toBe "RegExp"
+ expect( Object.class_name_of(new Object(/a/)) ).toBe "RegExp"
+ expect( Object.class_name_of(new RegExp("a")) ).toBe "RegExp"
+
+ it "distinguishes Boolean", ->
+ expect( Object.class_name_of(true) ).toBe "Boolean"
+ expect( Object.class_name_of(new Object(true)) ).toBe "Boolean"
+ expect( Object.class_name_of(1==0) ).toBe "Boolean"
+ expect( Object.class_name_of(new Boolean(true)) ).toBe "Boolean"
+
+ it "distinguishes User class", ->
+ class A
+ expect( Object.class_name_of(A) ).toBe "Function"
+ expect( Object.class_name_of(new A) ).toBe "A"
+ class B extends A
+ expect( Object.class_name_of(B) ).toBe "Function"
+ expect( Object.class_name_of(new B) ).toBe "B"
+
+ it "finds constructor stored in a global variable", ->
+ root.global_func = ->
+ expect( Object.class_name_of( new global_func() ) ).toBe "global_func"
+
+ # for simplified version
+ #
+ # it "does not find constructor stored in a global variable", ->
+ # root.global_func = ->
+ # expect( Object.class_name_of( new global_func() ) ).toBe "anonymous"
+
+ it "does not find constructor stored in a local variable", ->
+ local_func = ->
+ expect( Object.class_name_of( new local_func() ) ).toBe "anonymous"
+
+ it "returns variety of names for DOM objects depending on the environment", ->
+ expect( Object.class_name_of( window ) ).toMatch /^(Window|constructor|anonymous)$/
+ expect( Object.class_name_of( document ) ).toMatch /^(HTMLDocument|XMLDocument|anonymous)$/
+ expect( Object.class_name_of( location ) ).toMatch /^(Location|Object|anonymous)$/
+
+ it "can find super class via __super__ for Coffee Script' classes", ->
+ class A
+ class B extends A
+ a = new A
+ b = new B
+
+ cn_of = Object.class_name_of
+ expect(cn_of A ).toBe "Function"
+ expect( A.__super__ ).toBeUndefined()
+ expect( a.constructor ).toBe A
+ expect(cn_of a.constructor ).toBe "Function"
+ expect( a.constructor.__super__ ).toBeUndefined()
+ expect(cn_of a ).toBe "A"
+ expect(cn_of B ).toBe "Function"
+ expect(cn_of B.__super__ ).toBe "A"
+ expect( B.__super__.constructor ).toBe A
+ expect( B.__super__.constructor.__super__ ).toBeUndefined()
+ expect( b.constructor ).toBe B
+ expect(cn_of b.constructor ).toBe "Function"
+ expect(cn_of b.constructor.__super__ ).toBe "A"
+ expect( b.constructor.__super__.constructor ).toBe A
+ expect( b.constructor.__super__.constructor.__super__ ).toBeUndefined()
+
+ it "returns only the last part of nested names", ->
+ class A
+ class A.B
+ expect(Object.class_name_of new A.B).toBe "B"
+
+ it "returns dummy name of a named function expression", ->
+ # http://bonsaiden.github.io/JavaScript-Garden/#function.general
+ C = `function D(){}`
+ expect(Object.class_name_of new C).toBe "D"
+
+ it "faces some problems with IE8", ->
+ cn_of = Object.class_name_of
+ # this is OK
+ expect(cn_of new Error() ).toBe "Error"
+ # IE8 returns "anonymous" for these Error objects
+ expect(cn_of new EvalError() ).toMatch "anonymous|EvalError"
+ expect(cn_of new TypeError() ).toMatch "anonymous|TypeError"
+ expect(cn_of new RangeError() ).toMatch "anonymous|RangeError"
+ expect(cn_of new ReferenceError() ).toMatch "anonymous|ReferenceError"
+ expect(cn_of new SyntaxError() ).toMatch "anonymous|SyntaxError"
+ expect(cn_of new URIError() ).toMatch "anonymous|URIError"
+
* コメント・質問 [#k42bd4cf]
-#article_kcaptcha
+#article();