Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 719101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:36:13+00:00 2026-05-14T05:36:13+00:00

I’m working on a Javascript class based on the Prototype library. This class needs

  • 0

I’m working on a Javascript class based on the Prototype library. This class needs to observe an event to perform a drag operation (the current drag-drop controls aren’t right for this situation), but I’m having problems making it stop observing the events.

Here’s a sample that causes this problem:

var TestClass = Class.create({
    initialize: function(element) {
        this.element = element;
        Event.observe(element, 'mousedown', function() {
            Event.observe(window, 'mousemove', this.updateDrag.bind(this));
            Event.observe(window, 'mouseup', this.stopDrag.bind(this));
        });
    },
    updateDrag: function(event) {
        var x = Event.pointerX(event);
        var y = Event.pointerY(event);
        this.element.style.top = y + 'px';
        this.element.style.left = x + 'px';
    },
    stopDrag: function(event) {
        console.log("stopping drag");
        Event.stopObserving(window, 'mousemove', this.updateDrag.bind(this));
        Event.stopObserving(window, 'mouseup', this.stopDrag.bind(this));
    }
});

Without .bind(this) then this.element is undefined, but with it the events don’t stop being observed (the console output does occur though).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:36:14+00:00Added an answer on May 14, 2026 at 5:36 am

    bind returns a new function reference every time you call it (that’s its job 🙂 ), and stopObserving will only unhook the handler if the function reference is an === match.

    To fix this, remember the event handler you bound as a property and then use that property with stopObserving. Or, if you’re in charge of that element, you can unhook all handlers for the mousemove and mouseup events by simply leaving off the third parameter. (See the linked docs for more about leaving off parameters to stopObserving).

    So either:

    initialize: function(element) {
        this.element = element;
        this.boundUpdateDrag = this.updateDrag.bind(this);
        this.boundStopDrag = this.stopDrag.bind(this);
        Event.observe(element, 'mousedown', function() {
            // Off-topic, but see note at end of answer, unrelated bug here
            Event.observe(window, 'mousemove', this.boundUpdateDrag);
            Event.observe(window, 'mouseup', this.boundStopDrag);
        });
    },
    stopDrag: function(event) {
        console.log("stopping drag");
        Event.stopObserving(window, 'mousemove', this.boundUpdateDrag);
        Event.stopObserving(window, 'mouseup', this.boundStopDrag);
    }
    

    Or just

    stopDrag: function(event) {
        console.log("stopping drag");
        Event.stopObserving(window, 'mousemove');
        Event.stopObserving(window, 'mouseup');
    }
    

    But note that the latter removes all handlers for those events on that element (well, the ones hooked up via Prototype).


    Off-topic, but note that there’s a bug in your initialize function: It’s using this in the handler for mousedown, but not ensuring that this is set to what it should be set to. You’ll need to bind that anonymous function, or use a variable in initialize to take advantage of the fact that that anonymous function is a closure.

    So either use bind again:

    initialize: function(element) {
        this.element = element;
        this.boundUpdateDrag = this.updateDrag.bind(this);
        this.boundStopDrag = this.stopDrag.bind(this);
        Event.observe(element, 'mousedown', (function() {
            Event.observe(window, 'mousemove', this.boundUpdateDrag);
            Event.observe(window, 'mouseup', this.boundStopDrag);
        }).bind(this));
    },
    

    Or use the fact you’re defining a closure anyway:

    initialize: function(element) {
        var self;
    
        self = this; // Remember 'this' on a variable that will be in scope for the closure
        this.element = element;
        this.boundUpdateDrag = this.updateDrag.bind(this);
        this.boundStopDrag = this.stopDrag.bind(this);
        Event.observe(element, 'mousedown', function() {
            // Note we're using 'self' rather than 'this'
            Event.observe(window, 'mousemove', self.boundUpdateDrag);
            Event.observe(window, 'mouseup', self.boundStopDrag);
        });
    },
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 440k
  • Answers 440k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not sure if this is exactly what you're after, but… May 15, 2026 at 5:07 pm
  • Editorial Team
    Editorial Team added an answer Yes; you need to use XML entities: <li>, etc. You… May 15, 2026 at 5:07 pm
  • Editorial Team
    Editorial Team added an answer It is called steganography. The Wikipedia page I linked to… May 15, 2026 at 5:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.