jQuery.fn 是什么意思? [英] What does jQuery.fn mean?

查看:24
本文介绍了jQuery.fn 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的fn是什么意思?

jQuery.fn.jquery

推荐答案

在 jQuery 中,fn 属性只是 prototype 属性的别名.

In jQuery, the fn property is just an alias to the prototype property.

jQuery 标识符(或$)只是一个构造函数,所有用它创建的实例都继承自构造函数的原型.

The jQuery identifier (or $) is just a constructor function, and all instances created with it, inherit from the constructor's prototype.

一个简单的构造函数:

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

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

一个类似于 jQuery 架构的简单结构:

A simple structure that resembles the architecture of 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"

这篇关于jQuery.fn 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆