interesting jQuery stuff
Friday, 9. March 2007, 14:12:59
The latest version of the library is available so you can see what I'm talking about.
A function can call itself recursively as constructor
Just inside the jQuery function we find:
if ( window == this ) return new jQuery(a,c);
This clever little trick means you don't need to use the "new" keyword all over your code to create a new jQuery object. You can just do
var obj=jQuery()and the this check will detect that it wasn't called as a constructor and call itself recursively, this time using the "new" keyword to define the expected jQuery object.
I have a question about the if clause though - it presumably means trouble if you change the "this" object and use something else than window. Say,
var obj = jQuery.call(document)would probably break something. I don't know jQuery well enough to tell if that would be a problem, but the check could perhaps be
if(this.constructor!=arguments.callee)to catch all cases?
Using || operator to default to an argument inside a function call
You may be used to seeing the || operator being used to provide a default value for a variable. For example
var username=prompt('Your name please')||'anonymous' will set the variable to "anonymous" if the user cancels the prompt or doesn't type any value.It still took me a while to understand what is going on here:
return this.setArray( // HANDLE: $(array) a.constructor == Array && a || // HANDLE: $(arraylike) // Watch for when an array-like object is passed as the selector (a.jquery || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType) && jQuery.makeArray( a ) || // HANDLE: $(*) [ a ] );
The basic idea is: if the object a is an array, we pass it to the function directly. If it is an object that is somewhat similar to an array (for example a NodeList), we pass the result of calling the jQuery.makeArray function which will take all the elements/nodes and add them to an actual array. Otherwise, we just pass an array containing nothing but the object a.
The key to understanding this is to understand that the statement
a.constructor == Array && aactually "returns" a if the first comparison is true. It is probably better phrased as "evaluates to a" but it may be easier explained as "returns".
Similarly,
(a.jquery || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType) && jQuery.makeArray( a )will go through all the conditions, and if they all hold true return the result of calling the makeArray function.
The || operator ties it all together and ensures the first clause that evaluates to something is chosen as input to the function.
You can use Array.push to add array elements to any object
The next interesting snippet is inside the setArray function itself:
setArray: function( a ) {
this.length = 0;
[].push.apply( this, a );
return this;
}Look at [].push.apply( this, a );. What happens here is that the array literal's "push" method is used to push element a onto the this object which isn't an array but a jQuery object. That's right, push is generic and can be used with any object. It means the object gets a ".length" property like an array has and can be iterated with for loops. Example:
var obj = new Object(); /* Yes: an object, not an array. */
var str='';
Array.prototype.push.call(obj, 'Hello');
Array.prototype.push.call(obj, ' World');
for(var i=0;i<obj.length;i++){
str+=obj[i];
}
alert(str);Array methods being generic is part of the amazing flexibility of JavaScript, but this feature of the language might not be well known among authors.
About the jQuery code, I wonder if their approach of creating an empty array literal to use its push method is slower or faster than my Array.prototype lookup. Perhaps the question will inspire a reader to do some performance testing?
Edit: but seems that they don't support anything lower than IE 6.
jQuery test results:
By FataL, # 9. March 2007, 16:47:12
Many of the other ones fail the same way in Firefox so might be an error in the tests.
By hallvors, # 9. March 2007, 17:54:25
And about the testsuite: It is also part of the bug tracking. When I can't solve a particular bug I investigated, I leave the test for others to refer to.
The list of supported browsers is close to YUI's list of A-grade browser. It's pretty difficult to support more then those without bloating the library even more with tons of workarounds.
I'm curious if Opera 9.20 fixes a particular bug I have with select-elements. I've got a testsuite for my validation jQuery plugin that shows that error: http://jquery.bassistance.de/validate/demo-test/testx.html
Regards
Jörn Zaefferer
By Enchos, # 9. March 2007, 18:09:24
I was just being curious about the window==this bit. I don't know jQuery well enough to be anywhere near suggesting a use case for jQuery.call()
Regarding the SELECT element issue from your test page, that is caused by Opera's WebForms2 implementation and thus isn't a bug. Have a look at this:
http://www.whatwg.org/specs/web-forms/current-work/#selectSeeding
By hallvors, # 9. March 2007, 21:56:57
By hallvors, # 9. March 2007, 22:01:28
By crisp, # 11. March 2007, 00:11:03
By _Grey_, # 11. March 2007, 02:31:49
function Foo()
{
if(this.constructor!=arguments.callee)
alert('Not called as a constructor!');
else
alert('Called as a constructor!');
}
Foo();
new Foo();
Foo.prototype =
{
bar: function()
{
alert('something');
}
}
Foo();
new Foo();
By crisp, # 11. March 2007, 22:25:59
After some thought (and some more experiments) I came up with the overcomplicated condition
This fits your case (cross-browser), but it still doesn't cover this case:
Foo.call({})This would perhaps be best?:
By _Grey_, # 12. March 2007, 04:27:16
By crisp, # 13. March 2007, 07:45:30
By hallvors, # 14. March 2007, 15:49:35
@crisp: Well, as long as you keep the right constructor...
By _Grey_, # 18. March 2007, 23:55:53
By crisp, # 22. March 2007, 00:04:06
By _Grey_, # 22. March 2007, 00:31:14
Foo.prototype.bar = function() {}
Foo.prototype.woei = function() {}
I do prefer something along:
Object.extend(Foo.prototype,
{
bar: function() {},
woei: function() {}
});
By crisp, # 26. March 2007, 00:38:08
By _Grey_, # 26. March 2007, 03:31:13
By crisp, # 29. March 2007, 21:56:37