Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does prototype mean here in the jQuery source code?

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;         ..........     }, }; 

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.

But I cannot understand the usage in the above snippet from jQuery.

Are there any canonical documents describing the prototype?

Thanks.

like image 863
liam xu Avatar asked Oct 30 '12 13:10

liam xu


People also ask

What does prototype mean in JavaScript?

The prototype is an object that is associated with every functions and objects by default in JavaScript, where function's prototype property is accessible and modifiable and object's prototype property (aka attribute) is not visible. Every function includes prototype object by default.

What does __ proto __ mean in JavaScript?

__proto__ is a way to inherit properties from an object in JavaScript. __proto__ a property of Object. prototype is an accessor property that exposes the [[Prototype]] of the object through which it is accessed. POSTly is a web-based API tool that allows for fast testing of your APIs (REST, GraphQL).

What is prototype and __ proto __ in JavaScript?

prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype. Current standards provide an equivalent Object.

What is prototype in JavaScript Freecodecamp?

The prototype property is an object which contains a constructor property and its value is Point2D function: Point2D.prototype.constructor = Point2D . And when you call Point2D with new keyword, newly created objects will inherit all properties from Point2D.prototype .


1 Answers

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" 

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.

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.

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); 

And if you look at jQuery.fn.init:

jQuery.fn.init.prototype = jQuery.fn; 

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.

like image 161
James Allardice Avatar answered Oct 12 '22 12:10

James Allardice