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

  • SEARCH
  • Home
  • 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 7926527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:48:40+00:00 2026-06-03T18:48:40+00:00

I’ve never really used JavaScript, but I know roughly what it’s about. Now I’m

  • 0

I’ve never really used JavaScript, but I know roughly what it’s about. Now I’m looking through some examples of chrome extensions, and I see this “pattern” quite a lot.

var Main = {
    enable: function(){ window.addEventListener('mousemove', onMouseMove, false); },
    onMouseMove: function(event){ _onMouseMove(event) },
    _onMouseMove: function(event){
        ...lenghty implementation...
    }
}

My question is, why? Is this some kind of optimization?

  • 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-06-03T18:48:43+00:00Added an answer on June 3, 2026 at 6:48 pm

    You’re question is a little vague; but I am guessing you’re asking why the developer has two onMouseMove methods instead of just doing all the work in one, ie:

    var Main = {
        onMouseMove: function(event) { 
            // Why not just have the implementation here?
            // Why are we delegating it to the other method?
            _onMouseMove(event) 
        },
        _onMouseMove: function(event){
            // ...length implementation...
        }
    }
    

    The answer is because of how scope is handled in JavaScript. In a nutshell, the this keywork in most classical OOP languages (like Java) always refers to the parent class (Main) in the scope of a Function – JavaScript doesn’t work like this.

    As there are no classical classes in JavaScript, the this keyword actually refers to the function which invoked it; that’s why the new keyword makes such a difference when creating a new object via its constructor function; for example:

    function MyConstructor = function () { 
        // Assign a member property
        this.aProperty = "An Example";
    }
    
    // Using the new keyword; a new scope is created for the call which refers to the
    // object about to be created (and returned).
    var withNew = new MyConstructor();
    console.log(withNew.aProperty);  // 'An Example'
    
    // Without the new keyword...
    var withoutNew = MyConstructor();
    console.log(withoutNew.aProperty);  // undefined
    
    // Because we didn't use new, the calling function scope was applied, so 
    // the `this` keyword resolves caller's scope.
    console.log(this.aProperty)  // 'An Example'
    

    By delegating from onMouseMove to _onMouseMove the scope remains bound to the Main object instead of being bound to the object which triggered the mouse event. Another, more readable way of achieving this is to use a delegate, or if you’re using ES5, Function.bind

    var Main = {
        enable: function() {
            window.addEventListener('mousemove', onMouseMove.bind(this), false); 
        },
        onMouseMove: function(event) { 
            // ...lengthy implementation...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.