gpt4 book ai didi

javascript - jQuery.fn 是什么意思?

转载 作者:IT老高 更新时间:2023-10-28 11:14:18 31 4
gpt4 key购买 nike

这里的fn是什么意思?

jQuery.fn.jquery

最佳答案

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

jQuery 标识符(或$)只是一个构造函数,用它创建的所有实例都继承自构造函数的原型(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架构的简单结构:

(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"

关于javascript - jQuery.fn 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4083351/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com