News for hackers. Stuff that matters.
Re:Why I still prefer Prototype to jQuery by fh on Monday January 12, 2009 @05:37PM (#431257)
Not having extensively used either jQuery or Prototype, I found this comparison interesting. But the entire discussion about jQuery "stealing" `this` is somewhat misinformed. Javascript closures don't caputure the value of `this`; instead, the value of `this` is determined at the call site. If the function is called as a method (using the `object.method()` syntax), `this` is bound to `object`; otherwise, `this` is bound to the global object (called `window` in browsers). The behavior I just described makes `this` is almost useless in Javascript closures, and the "idiomatic Prototype" example the author gives cannot possibly work:

    // Prototype
    $A(this.columns).each(function(item) {
        this.buildHeader(item);
    });
If you want to do that, you have explicitly bind `this` to a (differently named) variable in the outer scope:

    var self = this;
    $A(self.columns).each(function(item) {
        self.buildHeader(item);
    });
Re:Why I still prefer Prototype to jQuery by litewulf on Monday January 12, 2009 @06:09PM (#431294)
I will say this, use of this is very scary and requires me to look at the docs more often (need to know where in the scope chain this is being set).

In fact, I'd argue that in general seeing uses of this outside of object oriented code make me sit up and look very carefully, because its really easy to get spooky "action at a distance" type behaviour.

Re:Why I still prefer Prototype to jQuery by richcollins on Monday January 12, 2009 @05:48PM (#431273)
Or pass in an object to the function that will be bound to this.

$A(self.columns).each(function(item) { this.buildHeader(item); }, this);

In general I prefer the technique you described as it is the most frequent use case and simplifies the implementation of the function that executes the callback.

Re:Why I still prefer Prototype to jQuery by dantheman on Monday January 12, 2009 @06:03PM (#431286)
or you can use javascript call or apply

  method.call (<object you want to be *this*>, params);
Re:Why I still prefer Prototype to jQuery by axod on Tuesday January 13, 2009 @05:26AM (#431883)
>> "The behavior I just described makes `this` is almost useless in Javascript closures"

Hmm??

e.onmouseover = function(c) {return function() { c.dosomething()}}(this);

Then in the dosomething function, "this" references what you expect...

I don't see why you need a library to construct a simple closure.

Re:Why I still prefer Prototype to jQuery by fh on Tuesday January 13, 2009 @07:50AM (#431968)
Note how you haven't used the keyword `this` inside the closure. What you've written is functionally equivalent to the technique I described, assigning the `this` object to a temporary variable. The only difference is that you bind it with a lambda instead of a `var`.
Re:Why I still prefer Prototype to jQuery by axod on Tuesday January 13, 2009 @09:01AM (#432023)
I don't use the keyword this inside the closure, but inside the function dosomething() I can happily use "this".
Re:Why I still prefer Prototype to jQuery by old-gregg on Monday January 12, 2009 @05:50PM (#431278)
I don't understand why does one need to make a choice. Even comparing the two makes little sense: jQuery is smaller and DOM-focused, while Prototype is a much more comprehensive multi-file library and DOM-related stuff is just a part of it, completely isolated in its own file which you can exclude during Prototype's build process.

I use both: jQuery for HTTP and DOM traversing/manipulation plus DOM-less prototype core for everything else. This combo is awesome, conflict-free and quite compact when gzipped. Bare bone JavaScript is too weak library wise and Prototype makes an excellent "standard library" for it. I couldn't live without Prototype's extensions to enumerable, strings, arrays, hashes, etc.

And it's 100% compatible with Rails thanks to jRails project. I highly recommend it.

Re:Why I still prefer Prototype to jQuery by litewulf on Monday January 12, 2009 @06:17PM (#431306)
One argument against doing both is that Prototype alters many built in classes directly. This is often risky and dangerous, and makes interactions between the two code bases possible.
Re:Why I still prefer Prototype to jQuery by jpcx01 on Monday January 12, 2009 @07:42PM (#431417)
Prototype alters stuff but jQuery doesn't. So there is very little conflict. The use of Prototype however will prevent use of other altering js frameworks like MooTools.

Fundamentally I think Prototype/MooTools are horribly designed javascript library because they introduce incompatibilities. However I use them when I need a free component or 2. I would never drop jQuery though so I always use $j instead of $, even when using jquery by itself

Re:Why I still prefer Prototype to jQuery by old-gregg on Monday January 12, 2009 @09:16PM (#431509)
A correction: it's better to say "Prototype expands built-in classes", i.e. it adds methods that otherwise aren't there. This is never risky nor dangerous unless you have another global-namespace library, which jQuery isn't.
Re:Why I still prefer Prototype to jQuery by tlrobinson on Monday January 12, 2009 @07:12PM (#431380)

    Determine the index of the first parameter in the Array …
    Ah, it returns the index. But that’s OK … JavaScript supports the notion of “truthiness”,
Except if it returns "0", of course.

It's a very C-like way of doing things:

   if ($.inArray(blah blah) < 0)
        // not found!
Re:Why I still prefer Prototype to jQuery by jkkramer on Monday January 12, 2009 @05:12PM (#431236)
It's nice to see a level-headed assessment like this. Too many programmers rely on trends or religious convictions to make decisions. All choices involve trade-offs, so make yourself aware of them!
Re:Why I still prefer Prototype to jQuery by noodle on Monday January 12, 2009 @05:25PM (#431250)
it is true that all choices involve trade-offs, and one library is going to likely be "best" for you based on your individual needs.

but i wouldn't call this article level-headed, it has an explicitly stated tilt towards prototype.

Re:Why I still prefer Prototype to jQuery by jgfoot on Monday January 12, 2009 @08:24PM (#431461)
JavaScript 1.6 added seven new array methods (indexOf, lastIndexOf, every, filter, forEach, map, and some). If only backward compatibility weren't a problem, the warring JavaScript libraries could just use these (or implement them into the prototype like Sugar Arrays, http://www.dustindiaz.com/sugar-arrays/) instead of creating their own quirky methods.
Re:Why I still prefer Prototype to jQuery by Caged on Monday January 12, 2009 @05:48PM (#431271)
The Prototype this example won't work unless this is bound to the anonymous function, otherwise this is the window object.

  $A(this.columns).each(function(item) {
    this.buildHeader(item);
  }.bind(this));
Re:Why I still prefer Prototype to jQuery by litewulf on Monday January 12, 2009 @06:11PM (#431300)
Based on the use of "this.columns" I assume this is bound to something. Probably not the anonymous function though.
Re:Why I still prefer Prototype to jQuery by larrywright on Monday January 12, 2009 @10:53PM (#431605)
I don't have a huge amount of experience with either framework, but I've used them both. One thing that stands out about jQuery is that it's unobtrusive - Prototype is not (at least not without some help from something like Lowpro).
Re:Why I still prefer Prototype to jQuery by muitocomplicado on Wednesday January 14, 2009 @04:37AM (#433449)
One of his arguments was that prototype is not that slow anymore in comparison to jQuery.

Some nice speed improvements are coming to jQuery 1.3:

http://www.flickr.com/photos/jeresig/sets/72157612498756067/

Re:Why I still prefer Prototype to jQuery by deadwisdom on Tuesday January 13, 2009 @01:55PM (#432453)
jQuery is light, but it has a trove of plugins that will help you with more complex tasks. The core is purposefully limited in scope, not trying to do everything for you; it only provides what Javascript already should, but doesn't.
Re:Why I still prefer Prototype to jQuery by hs on Monday January 12, 2009 @11:18PM (#431621)
i prefer framework that helps me crafting shorter code

the last time i used prototype was in 2007, now it's all jquery

Re:Why I still prefer Prototype to jQuery by GrandMasterBirt on Monday January 12, 2009 @07:54PM (#431432)
Argument order:

jQuery

$.each(collection,function(){ this.bla(); });

The reason index is first param is because wheb you need it it is the only param. The reason for the item param is incase you need to attach a scope object to be this!