原型在jQuery源代码中意味着什么? [英] What does prototype mean here in the jQuery source code?
问题描述
例如,从jQuery 1.2.6复制而来:
As an example, copied from jQuery 1.2.6:
jQuery.fn = jQuery.prototype = {
init: function( selector, context ) {
// Make sure that a selection was provided
selector = selector || document;
..........
},
};
我在这里阅读了一些帖子,例如> JavaScript:什么是.extend和.prototype用于?,并且知道可以在子类中使用原型来扩展某些方法.
I have read some posts here like JavaScript: What are .extend and .prototype used for? and know a prototype can be used in a subclass to extend some methods.
但是我无法理解上述jQuery片段中的用法.
But I cannot understand the usage in the above snippet from jQuery.
是否有描述原型的规范文档?
Are there any canonical documents describing the prototype?
谢谢.
推荐答案
所有对象都具有prototype
属性.它只是一个对象,其他对象可以从中继承属性.您发布的代码片段只是将具有某些属性(例如init
)的对象分配给jQuery
的prototype
,并将别名jQuery.prototype
分配给jQuery.fn
,因为fn
较短且键入更快.如果您暂时忘记了jQuery,请考虑以下简单示例:
All objects have a prototype
property. It is simply an object from which other objects can inherit properties. The snippet you have posted simply assigns an object with some properties (such as init
) to the prototype
of jQuery
, and aliases jQuery.prototype
to jQuery.fn
because fn
is shorter and quicker to type. If you forget about jQuery temporarily, consider this simple example:
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function () {
alert(this.name + " says hello");
};
var james = new Person("James");
james.sayHello(); // Alerts "James says hello"
在此示例中,Person
是构造函数函数.可以通过new
运算符对其进行实例化.在构造函数内部,this
关键字引用实例,因此每个实例都有自己的name
属性.
In this example, Person
is a constructor function. It can be instantiated by calling it with the new
operator. Inside the constructor, the this
keyword refers to the instance, so every instance has its own name
property.
Person
的prototype
在所有实例之间共享.因此,所有Person
实例都有一个sayHello
方法,它们从Person.prototype
继承 .通过将sayHello
方法定义为Person.prototype
的属性,我们可以节省内存.我们可以很容易地给Person
的每个实例自己的方法副本(通过将其分配给构造函数中的this.sayHello
),但是效率不高.
The prototype
of Person
is shared between all instances. So all instances of Person
have a sayHello
method that they inherit from Person.prototype
. By defining the sayHello
method as a property of Person.prototype
we are saving memory. We could just as easily give every instance of Person
its own copy of the method (by assigning it to this.sayHello
inside the constructor), but that's not as efficient.
在jQuery中,当您调用$
方法时,您实际上是在创建jQuery.prototype.init
的实例(记住jQuery.fn === jQuery.prototype
):
In jQuery, when you call the $
method, you're really creating an instance of jQuery.prototype.init
(remember that jQuery.fn === jQuery.prototype
):
return new jQuery.fn.init(selector, context, rootjQuery);
如果您查看jQuery.fn.init
:
jQuery.fn.init.prototype = jQuery.fn;
实际上,您正在创建jQuery
的实例,该实例可以访问在jQuery.prototype
上声明的所有方法.如前所述,这比在jQuery
的每个实例上声明这些方法要有效得多.
So really, you're creating an instance of jQuery
which has access to all the methods declared on jQuery.prototype
. As discussed previously, this is much more efficient than declaring those methods on each instance of jQuery
.
这篇关于原型在jQuery源代码中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!