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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:55:30+00:00 2026-05-16T13:55:30+00:00

In my javascript objects i found myself writing this: this_object = this; It seems

  • 0

In my javascript objects i found myself writing this:

this_object = this;

It seems it’s the only way to pass member variables to external functions…

google.maps.event.addListener(this.marker, 'click', function() {
    this.info_window.setContent('Chicago marker');
    this.info_window.open(this.map,this.marker);
});

That doesn’t work, I have to copy the object into a member variable and pass the new object (and replace all this with this_object)

This feels ugly. Is there a “better” or “cleaner” way, or is this my only option?

  • 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-16T13:55:30+00:00Added an answer on May 16, 2026 at 1:55 pm

    Sure there is a better method. It involves creating a function which has the this context already bound to a particular object.

    To have the this context refer to the current object, call the bind() method on the function and pass the required context as a parameter.

    google.maps.event.addListener(this.marker, 'click', function() {
        this.info_window.setContent('Chicago marker');
        this.info_window.open(this.map,this.marker);
    }.bind(this)); // <-- notice we're calling bind() on the function itself
    

    This is now part of the ECMAScript standard, and if a browser does not implement it natively, it’s easy to do it yourselves.

    if (!Function.prototype.bind) {
        Function.prototype.bind = function () {
            var fn = this,
                args = Array.prototype.slice.call(arguments),
                object = args.shift();
    
            return function () {
                return fn.apply(
                    object, args.concat(Array.prototype.slice.call(arguments))
                );
            };
        };
    }
    

    See all questions and answers on SO related to this.

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

Sidebar

Related Questions

Does Javascript supports Sets(list with unique objects only) ? I have found this link
I just found out the hard way objects are passed by reference in Javascript,
Going through Javascript documentation, I found the following two functions on a Javascript object
I've read that using Object.prototype to attach functions to custom JavaScript objects is more
I wonder if there is a way to synchronize objects/methods in JavaScript in a
I've always found it interesting that in JavaScript you can actually extend functions into
For using a static variable in javascript functions I found out two ways, using
I'm new to javascript namespaces, and I found myself kinda deep within a namespace,
I need to transfer JavaScript Objects through JSON and transfer it's functions as well.
The tutorial is here: Tutorial: Writing Spreadsheet data using JavaScript Objects The full code

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.