Skip to content

Commit

Permalink
feat(jQuery): どういう用途に向いてる/向いていない を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 31, 2015
1 parent 13f26de commit ce88188
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion ja/jQuery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,26 @@ jQuery.fn = jQuery.prototype = {
$(document.body); // 返り値はjQueryのインスタンス
```


つまり、jQueryプラグインはJavaScriptのprototypeをそのまま利用しているだけに過ぎないということがわかります。

## どういう用途に向いている?

jQueryプラグインの仕組みがわかったのでどういう用途に有効な仕組みなのか考えてみましょう。

単純なprototype拡張であると言えるので、利点はJavaScriptのprototypeと同様と言えるかもしれません。
動的にメソッドを追加するだけではなく、既存の実装を上書きするmonkey patchのようなものもプラグインとして追加することができます。

## どういう用途に向いていない?

これもJavaScriptのprototypeと同様で、prototypeによる拡張は柔軟すぎるため、
jQuery自体がプラグインのコントロールをすることが難しいです。

また、プラグインが拡張するjQueryの実装に依存し易いため、
jQueryのバージョンによって動かなくなるプラグインが発生しやすいです。

jQueryではそこをどうやってカバーしているかというと、
ドキュメント化されてないAPIは触っていけないというルールを設けているだけとなっています。

## 実装してみよう

`calculator`という拡張可能な計算機をjQuery Pluginと同じ方法で作ってみたいと思います。
Expand Down

0 comments on commit ce88188

Please sign in to comment.