履歴: プログラミング/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/11/27 15:06:40) → 比較: プログラミング/CoffeeScript (2014/5/29 16:05:27)

@@ -8,8 +8,8 @@
もっと簡単に JavaScript を書きたい、という人向けに作られた言語だそうです。
-[[Play Framework>ソフトウェア/PlayFramework/チュートリアル+α]] には自動的に JavaScript に変換してくれる機構が備わっているので
-シームレスに簡単に使うことができます
+最近の Ruby on Rails や [[Play Framework>ソフトウェア/PlayFramework/チュートリアル+α]] には自動的に JavaScript に変換してくれる機構が備わっているので
+シームレスに使うことができます
** メリット [#sd53cdfc]
@@ -518,6 +518,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