I thought I'd read somewhere on here about functions being destructive, but I wasn't sure hence the question. While it makes sense when you're doing crazy chained method calls, I think it's very unintuitive during regular use of the parent() method, especially because there's nothing which signifies in the method call that it is indeed destructive (such as in Ruby or Scheme where you have set! or what not). It's also not noted in the API... which is definitely an oversight no matter how you look at it.
Maybe someone can shed some light on specific situations where the destructive method saves considerable amounts of code. I can definitely see how it'd be useful--do some work on the parent, then end(), then do some work on the element itself--but is this functionality used frequently? I think it makes perfect sense for filter(), but even so, I think it should be made clear in some way or another that a function you're calling is destructive, otherwise it can create the incredibly confusing behavior I demonstrated above unless you're made explicitly aware that it's not doing what you think it's doing. Thanks for the answer nonetheless, and I apologize in advance to John and Jörn if this has been discussed to death before! Cheers! ~Peter On 10/17/06, Jörn Zaefferer <[EMAIL PROTECTED]> wrote: > Peter Woods schrieb: > > I'm not sure if this is the way it's supposed to work, but this makes > > absolutely no sense to me as far as implementation goes. If I have > > code like this: [...] > > > > Instead of printing the equivalent of: $(e).id(), $(e).parent().id(), > > and $(e).parent().parent().id(), it prints $(e).parent.parent.id() > > three times. Further digging seems to hint at the fact that every time > > cell.parent() is called, it's the equivalent of cell = cell.parent() > > and as such the original value of cell is lost. > > > > Is this behavior intentional, or is it a bug? It seems quite > > counterintuitive to me, and while in the dumbed-down example above it > > makes more sense to use $(e).parent() instead of cell.parent(), I find > > it more intuitive to use cell.parent() in other instances and thus > > discovered this behavior. > It is intentional. parent() is a destructive operation on the jQuery > object like filter(), find() etc. and can be reverted with end(). > > There is the concept to pass an additional function to all desctructive > operations: That function is executed for the modified stack and then > returns an unmodified object. Maybe the discussion should be started > again... There were several votes that said it would be unintuitive if a > single method is once destructive and once it is not because an > additional function is passed... What do you think? > > -- Jörn > > _______________________________________________ > jQuery mailing list > [email protected] > http://jquery.com/discuss/ > _______________________________________________ jQuery mailing list [email protected] http://jquery.com/discuss/
