[thelist] Javascript object / prototype question
John Lauck
recaffeinated at gmail.com
Thu Jan 10 13:58:25 CST 2008
I forgot to mention there's probably some ruby/erb code mixed into that js,
but it basically just sets some constants in the js file. I don't include
that file itself, it's actually wrapped with script tags in a rails view.
On Jan 10, 2008 1:52 PM, John Lauck <recaffeinated at gmail.com> wrote:
> Here's the script: http://recaffeinated.com/files/clawfoot.js
>
> So much of it deals with jQuery's event and selector methods it might not
> make as much sense unless you are aware of the jQuery API.
>
> Here's an example (at line 467):
>
> bind_choosable_click_event: function() {
> var assigner = this;
>
> assigner.get_choosable().unbind('click').click(function() {
> var element = $j(this);
> var options = {
> id: assigner.assigner_element_numeric_id({element:
> element}),
> element: element,
> type: assigner.assigner_element_type (element)
> };
>
> // passing the entire options obj to is_assigned here is okay,
> but it might be less overhead if I just sent type and id
> if(assigner.is_assigned(options)) {
> assigner.unassign(options);
> }else{
> assigner.assign(options);
> }
> });
> },
>
> You can see that `this` is replaced as soon as you open a function() {},
> so I save the current `this` in the `assigner` variable. Otherwise I
> wouldn't have a way to access it beyond that point (that I know of). If
> this means that I'm copying the current object into a new variable
> (recursively in a way, since it's within itself) it could be creating a huge
> stack of code in memory. I hope I'm incorrect. This is my first real
> attempt at creating a large prototype/object so it's possible my code is
> rough.
>
> Thanks,
>
> John
>
>
> On Jan 10, 2008 1:21 PM, Matt Warden <mwarden at gmail.com> wrote:
>
> > On 1/10/08, John Lauck <recaffeinated at gmail.com> wrote:
> > > Yes, that's the issue. I use jQuery and as soon as you open another
> > > function() { ... } you can no longer use your `this` variable. I
> > assumed
> > > that the new variable is just a reference, so it's not actually
> > copying the
> > > data to a new variable and that the reference is cleaned up when the
> > > function ends. Those are the two issues. However, I've seen it used
> > in
> > > lots of scripts, so unless there's some sort of cleanup I'm unaware
> > of?
> >
> > No, the only reason you would do this is because you do NOT want the
> > reference to be cleaned up once the function ends. Otherwise you would
> > just use 'this', right?
> >
> > Can you include more of your code?
> >
> > --
> > Matt Warden
> > Cincinnati, OH, USA
> > http://mattwarden.com
> >
> >
> > This email proudly and graciously contributes to entropy.
> > --
> >
> > * * Please support the community that supports you. * *
> > http://evolt.org/help_support_evolt/
> >
> > For unsubscribe and other options, including the Tip Harvester
> > and archives of thelist go to: http://lists.evolt.org
> > Workers of the Web, evolt !
> >
>
>
More information about the thelist
mailing list