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 7756281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:46:59+00:00 2026-06-01T12:46:59+00:00

I am beginning to define classes in JavaScript, and I have a lot of

  • 0

I am beginning to define classes in JavaScript, and I have a lot of troubles with the keyword this.

Here is an example of what I want to do:

function MyMap() {

    this.map = new google.maps.Map(....);

    google.maps.event.addListener(this.map, 'idle', function() {
        this.mapIdle();    // PROBLEM: "this" undefined
    });

    this.mapIdle = function() {
        google.maps.event.addListener(marker, 'click', function() {
            $("button").click(function() {
                $.ajax({
                    success: function() {
                        this.map.clearInfoWindows(); // PROBLEM: "this" undefined
                    }
                });
            });
        });
    }
}

As you can see in the comments, this won’t work here, because it is used inside a closure.

I have started using workarounds like:

var that = this;
google.maps.event.addListener(this.map, 'idle', function() {
    that.mapIdle();
});

Or even where you have to define a callback function around your callback function (seriously!!).

This is extremly ugly and doesn’t work everywhere. When I get a lot of nested lambda functions (as in the example I gave), I have no idea how to use a class attribute.

What is the best and most correct way to do that?

  • 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-01T12:47:00+00:00Added an answer on June 1, 2026 at 12:47 pm

    The easiest way is to define a self variable (or that, if you don’t mind a non-semantic variable name) as you already mentioned:

    function MyMap() {
       var self = this;
    
       // in nested functions use self for the current instance of MyMap
    }
    

    Noting that you have to do it again for methods you add to the prototype (if they use nested functions):

    MyMap.prototype.myMethod = function() {
       var self = this;
       // use self in nested functions
    };
    

    You should also read up on the .bind() method, noting that it doesn’t work for IE <= 8.

    The question you linked to about defining a callback around your callback is to solve a different problem, i.e., setting up an appropriate closure structure to allow functions that are nested inside a loop to have access to the appropriate loop counter value(s). That has nothing to do with the this issue. (And can easily be combined with the self technique when needed.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I'm feeling pretty thick right now. Basically, I want to define a JavaScript
After having written a few helper classes in magento, now i have this problem,
I have a jar file with 30 or so classes. What I want is
I have a powershell script I want to be able to define different starting
Since beginning to use VB.NET some years ago I have become slowly familiar with
I got the Wrox.Beginning.JavaScript.3rd.Edition and wanted to start learning it from scratch, then my
I am a beginning javascript programmer. I am trying to create something similar to
I have a list of checkpoints and then a run a function. I originally
at the beginning I have: #include <sstream> #include <iostream> #include <stdio.h> #include <iomanip> #include
In my application which uses spring security I want to define two different areas

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.