ES Harmony Proxy on Firefox 4

Preview:

Citation preview

ES Harmony

Proxyon

Firefox 4

id:mooz@stillpedant

About

mooz (@stillpedant)

Add-ons:

KeySnail

Emacs 版 Vimperator

Clipple

クリップボードマネージャ

gpum

Gmail の未読メール管理

See http://mooz.github.com/index-ja.html

What’s the ES Harmoney?

ECMAScript 3

ECMAScript 4

ECMAScript 5

ECMAScript Harmoney未来の JavaScript

What’s the Proxy?

• プロパティアクセス (代入, 取得)• in 演算子の適用• delete 演算子の適用• new F(), F()

などの操作がされた時,JavaScript 規定の動作ではなく

“ユーザの設定した関数”が呼び出されるようなオブジェクト

ES Harmoney (未来の JavaScript) の機能

• Proxy の作成には Proxy.create() を使用

Proxy.create()

ハンドリング時に呼ばれる関数ハンドルしたい操作

Example (1/3)

var x = obj.x;

get が呼ばれ name に “x” が入る.

Example (2/3)

obj.y = 100;

set が呼ばれ

name に “y”, val に 100 が入る.

Example (3/3)

(“foo” in obj)

has が呼ばれ name に “foo” が入る.

Proxy for?

Q: 「こんなものが何の役に立つの?」

A: 「組み込みオブジェクトを模倣できるよ」

B: 「DSL! DSL!」

夢が広がる

Proxy on Firefox 4

• Firefox 4 では Proxy が使用可能

Use cases

• 二つほど使用例をでっち上げることに成功

1.not

2.safe

1.not

• あるオブジェクト (親含む) が,

あるプロパティを持たない場合に実行したい

嬉しくない

Why not?

• obj を Proxy でラップ

• obj に対する in 演算子の効果を反転

嬉しい!

not(obj)

not :: Before & After

Before:

After:

not (implementation)

時に呼ばれる関数

結果を反転して返す

2. safe

• 深いオブジェクトのプロパティをたどりたい

– プロパティが途中で定義されていなかったりする

– null や undefined が出てきたりする

• 単純に obj.a.b.c と出来ない

やってられない

Why safe?

嬉しい!

safe(x)

• x を Proxy でラップ

• . の代わりに $ を使ってプロパティをたどる

• 安全

safe :: Before & After

Before:

After:

safe (implementation)

このProxy オブジェクトの get メソッドに

という文字列が渡る

で文字列を分割して得られたプロパティを順にたどる

(この中に面倒な処理を隠蔽)

Proxy 便利

でも、Firefox 4 限定でしょ?

宗教上の理由でFirefox

使えないし……

ご安心下さい

References

[[harmony:proxies]] http://wiki.ecmascript.org/doku.php?id=harmony:proxies

Proxy Tutorialhttp://soft.vub.ac.be/~tvcutsem/proxies/index.html

Proxies are Awesome!http://www.slideshare.net/BrendanEich/metaprog-5303821

ご清聴ありがとうございました

Recommended