履歴: プログラミング/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 (2014/5/28 15:58:53) → 比較: プログラミング/CoffeeScript (2016/3/9 4:20:09)
@@ -113,8 +113,11 @@
まあ、車輪の再発明っぽいところもあるわけですが、
祝日名が取れて、なおかつメンテナンスしやすい形の
-ライブラリが見付からなかったので。
+ライブラリが見付からなかったので書いてみました。
+現在はこちらで公開しています。
+[[ソフトウェア/javascript/japanese-holidays]]
+
一応、https://gist.github.com/Songmu/703311 と突き合わせて
齟齬がないことを確認しましたが、運用は個々の責任でお願いします。
@@ -123,6 +126,10 @@
*** ソース [#yac07e9e]
+COLOR(red){SIZE(40){''以下のコードは古いです''}}
+
+[[ソフトウェア/javascript/japanese-holidays]] に最新版がありますので、そちらを参照してください。
+
LANG:coffeescript(linenumber)
###
@@ -518,19 +525,45 @@
return elem
return null
-** Object.is_a [#l2369fea]
+** Object.is_a, Object::is_a [#l2369fea]
-null や undefined も判別できるように、obj.is_a(Class) ではなく Object.is_a(obj, Class) になっている。
+javascript の instanceof 演算子が壊れた仕様であることは有名ですが、
-ほぼ http://webreflection.blogspot.jp/2011/10/better-isa-function-for-js.html のコードそのままです
+参照: 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", ->
@@ -551,6 +584,7 @@
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", ->
@@ -583,37 +617,118 @@
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) の形になっています。
+こちらも 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)->
- get_name = (cons)->
- if /function\s+(\S+)\(/.exec cons.toString()
- return RegExp.$1
- "anonymous"
return "null" if obj == null
return "undefined" if obj == undefined
- result = get_name(obj.constructor)
- return result unless result == "anonymous" and
- obj.constructor.prototype?
- # search constructor in the global scope
- for key, value of root
- if value? and value.prototype? and
- (typeof value) == "function" and
- obj instanceof value
- return key
- return result
+ 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();
@@ -622,29 +737,43 @@
ではなく、
LANG:javascript
- MyClass = function(){};
+ MyClass = function(){}; // グローバルに宣言された変数
obj = new MyClass();
alert(Object.class_name_of(obj));
-とした場合にも名前を判別できるように工夫しているためです。
+とした場合にも名前を判別できるよう工夫しているためです。
+(たぶん、リンク先ではそのあたりのコードがおかしくなっているように思えるのですが・・・)
その際、グローバルオブジェクトのプロパティをスキャンしているため、
-無名クラスのオブジェクトに対して頻繁に呼び出すとパフォーマンス的に問題が生じる可能性があります。
+この関数を無名クラスのオブジェクトに対して頻繁に呼び出すと、
+パフォーマンス的に問題が生じる可能性があります。
-上記のような無名クラスのオブジェクトに対して "anonymous" を返して構わなければ、
-コメント部分の直前を return get_name(obj.constructor) とした次のコードで十分です。
+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
- "anonymous"
+ return "anonymous"
*** テスト [#v74d0bf8]
LANG:coffeescript
+ # for jasmine
root = exports ? this
describe "Object.class_name_of", ->
@@ -671,7 +800,6 @@
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"
- expect( Object.class_name_of(new Object(true)) ).toBe "Boolean"
it "distinguishes RegExp", ->
expect( Object.class_name_of(/a/) ).toBe "RegExp"
@@ -705,8 +833,57 @@
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