I wouldn't call it 'corruption'. It's just a different approach. In
jQuery 'this' will always refer to the element to which the method is
being applied. And the data() function doesn't add any properties to
the element.

Any reason to not simply take advantage of scoping?

myClass = function( element ) {
        this.element = element;

        var obj = this;
        this.onclick = function() {
               //this == element
               //obj == this "class' instance"
        }

        $(this.element).click( this.onClick );
};

jQuery follows a more functional approach that fits web apps and DOM
handling very well. The closest you'd get to a class in 'traditional'
jQuery is this:

jQuery.fn.myThing = function(){
    this.data('something', { some: 'thing', a: 2 })
         .click(function(){
            //onclick functions
            doSomethingWith( $(this).data('something') );
         });
};

$('#element1,#element2').myThing();

I suggest you take a deep breath and open your mind a bit. The
transition is hard, frameworks are fundamentally different, but that
doesn't make one inherently better or worse than the other.

cheers,
- ricardo

On Mar 4, 6:25 pm, Michael <dmgx.mich...@gmail.com> wrote:
> And jQuery docs criticize prototype for polluting the global namespace
> and in the same breath recommend binding element properties that would
> normally go on a class to elements with all the potential namespace
> collisions that brings?
>
> O... K...... (backs away slowly).
>
> Honestly, the more I use this framework the less I like it.  The
> corruption of 'this' is a major flaw in the framework I don't think
> I'll ever be happy with.
>
> On Mar 3, 10:32 pm, Dave Methvin <dave.meth...@gmail.com> wrote:
>
> > > one area of functionality has me befuddled
> > > - using object methods to handle events.
>
> > To continue Karl's point, if you use jQuery the same way you would use
> > prototype then you'll be fighting it all the way. The prototype
> > solution squirrels away references to the DOM objects in the class and
> > then points event handlers back to the class. jQuery usually
> > encapsulates the data and events within the DOM element or a closure,
> > e.g.:
>
> > $("selector")
> >   .data("options", {answer: 42})
> >   .click(function(){
> >      alert($(this).data("options").answer)
> >   });

Reply via email to