3

In the jQuery library, I found this line

jQuery.extend = jQuery.fn.extend = function() {

This kind of puzzles me. I thought that a given object automatically provides access to its prototype methods (in other words, if a method, or var, is not resolved on the object itself, it is tried on its prototype, then on the prototype's prototype and so on).

So, what's the reason behind associating the function both to the object and to the prototype? (in jQuery fn is an alias for prototype).

1 Answer 1

6

The magical prototype is a property of constructors, not objects. An instance of jQuery would inherit extend from the prototype, but jQuery needs its own copy.

The above is quite true, but perhaps misleading. All objects have an internal [[Prototype]] property that's used for property resolution, but it's not necessarily directly accessible within Javascript (see ECMA-262 § 8.6.2 an 4.2.1). You can access the prototype of an object via obj.constructor.prototype, but that doesn't use the [[Prototype]] property and is, in fact, something a little different. Some browsers (e.g. Firefox and Safari) support the non-standard __proto__ property that is probably [[Prototype]]. Since it's not a standard property, it's of limited usefulness.

Here's an illustration of from § 4.2.1 to help a little:

alt text http://img121.imageshack.us/img121/3504/prototypes.png

CF is a constructor; each cfi is created from CF. The solid arrow you see from CF to Cfp represents the prototype property you can access in Javascript. The dotted arrows represent references that each object has to its prototype and that the Javascript engine uses, but doesn't expose to scripts. Note that CF has its own implicit prototype link to Function.

Sign up to request clarification or add additional context in comments.

1 Comment

what do you mean with the last part ? how are you supposed to set on the prototype ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.