1. ホーム
  2. javascript

[解決済み] jQuery.fnとは何ですか?

2022-03-16 20:06:18

質問

とは何ですか? fn の意味は?

jQuery.fn.jquery

解決方法は?

jQueryでは fn プロパティは、単に prototype プロパティを使用します。

は、その jQuery 識別子(または $ ) は、単に コンストラクタ関数 このコンストラクタで生成されたすべてのインスタンスは、コンストラクタのプロトタイプを継承します。

単純なコンストラクタ関数です。

function Test() {
  this.a = 'a';
}
Test.prototype.b = 'b';

var test = new Test(); 
test.a; // "a", own property
test.b; // "b", inherited property

jQueryのアーキテクチャに似たシンプルな構造。

(function() {
  var foo = function(arg) { // core constructor
    // ensure to use the `new` operator
    if (!(this instanceof foo))
      return new foo(arg);
    // store an argument for this example
    this.myArg = arg;
    //..
  };

  // create `fn` alias to `prototype` property
  foo.fn = foo.prototype = {
    init: function () {/*...*/}
    //...
  };

  // expose the library
  window.foo = foo;
})();

// Extension:

foo.fn.myPlugin = function () {
  alert(this.myArg);
  return this; // return `this` for chainability
};

foo("bar").myPlugin(); // alerts "bar"