Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in jQuery core.js isn't extend defined with a prototype

Why in jQuery core.js isn't extend defined as like this:

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

and not as a prototype like:

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

Presumably with the former, objects created from jQuery will not have the extend function.

like image 749
rogermushroom Avatar asked Mar 28 '26 01:03

rogermushroom


2 Answers

Because jQuery.fn === jQuery.prototype

It is defined on the prototype. jQuery just decided it would be "cute" to alias the prototype to .fn

Which is why

$().extend({ 
    "lulz": "baz" 
}, { 
    "more-lulz": "no wai" 
})["more-lulz"] === "no wai"; // true
like image 169
Raynos Avatar answered Mar 29 '26 15:03

Raynos


Well, because fn is nothing than a shortcut to the prototype property :

console.log($.prototype === $.fn);

Maybe John Resig got bored of typing prototype for every method and set up a nice alias fn (which is indeed shorter and in my opinion more suggestive).

like image 38
gion_13 Avatar answered Mar 29 '26 15:03

gion_13