Skip to content

jQuery Plugin #8

Closed
Closed
@azu

Description

@azu

jQueryのプラグインについて

どう書ける?

<script src="jquery.js"></script>
<script src="something.js"></script>

と読み込ませると、jQueryオブジェクトで拡張したメソッド使えるようになる。

$('.class-name').something();

どういう仕組み?

$.fn に拡張したいメソッドを追加すると、jQueryオブジェクトにそのメソッドが追加される。
prototypeに対して拡張するのと同じ

$.fn.something = function() {
    console.log("something");
};

どういう事に向いてる?

  • jQueryのようなDOM要素をラップしたようなオブジェクトに適当なメソッドを増やすのに向いてる
  • <script>時代のものなので、複数のバージョンの扱いなど大雑把

どういう事に向いていない?

  • グローバルに$ or jQuery があるという前提という空気感
  • 結局はprototype拡張なのでjQueryという実体の構造に依存しがち

この仕組みを使ってるもの


チェックシート

  • どう書ける?
  • どういう仕組み?
  • どういう用途に向いている?
  • どういう用途に向いていない?
  • この仕組みを使ってるもの
  • 実装してみよう
  • 参考文献 Furture Reading
  • まとめ

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions