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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:45:06+00:00 2026-06-17T04:45:06+00:00

I come from C++, found out this only means execution context. Is there a

  • 0

I come from C++, found out “this” only means execution context. Is there a guaranteed way to get self instance?

I ask this because I always try to get instance by “this” in javascript, but I have to do various ways to guarantee it by myself, such as ways described as follow:

MyClass.prototype.OnSomethingHappened = function () {
  // I want to get the reference to the instance of this class.
}

But this kind of function is often called like:

var bar = new MyClass();
foo.onclick = bar.OnSomethingHappened;

When onclick happend, OnSomethingHappened get called, but “this” does not mean the instance of bar.

There are some solution like:

var bar = new MyClass();
foo.onclick = function () {
  bar.OnSomethingHappened();
}

Yes, it works perfectly here.
But consider:

var bar = new MyClass();
MyClass.prototype.OnSomethingHappened = function () {
  // I want to get the reference to the instance of this class.
}
MyClass.prototype.IWantToBindSomething = function () {
  // sorry for using jquery in a pure javascript question
  $("div#someclass").bind("click", function () {
  bar.OnSomethingHappened();
  });  // I think this is a very very bad practice because it uses a global variable in a class, but I can't think of other workaround, since I have no guaranteed way of getting the instance.
}
  • 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-17T04:45:08+00:00Added an answer on June 17, 2026 at 4:45 am

    No, the concept of a “class” really doesn’t exist in JavaScript the way it does in C++. There’s no intrinsic relationship between a function and any particular object. Functions are just values.

    (Well there’s no relationship other than the “casual” relationship when an object has a property that refers to a function.)

    However, it’s possible to force a particular value of this by wrapping a function in another function that uses .call() or .apply() to invoke the target function. You can do that with .bind() in newer browsers, or simply with an anonymous (or not-anonymous) wrapper.

    edit — what’s important to understand about this is that it’s value is completely determined by the way a function is invoked, at each point of invocation. Thus, if you have an object:

    var myObj = new Something();
    

    and you want to use myObj.handler as an event handler (common situation) such that this in the event handler refers to the object, you simply need a wrapper:

    $('whatever').click( function(ev) { myObj.handler(ev); } );
    

    In that example, it’s not necessary that “myObj” be a global variable:

    function justAnExample() {
      var myObj = new Something();
    
      $('whatever').click( function(ev) { myObj.handler(ev); } );
    }
    

    In that you can clearly see that “myObj” is a local variable inside the “justAnExample” function. Because JavaScript has real closures, the invocation context of “justAnExample” is preserved after a call, and it’s there for use by the anonymous wrapper function passed as the event handler to jQuery.

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

Sidebar

Related Questions

I copied come text from a The Huffington Post article and found that the
I come from a php background and in php, there is an array_size() function
Sorry, but I can't figure this out from the Python documentation or any of
I come from the Java world, so to me it's all object.foo() , but
I come from a Java background and am getting more into .NET, what are
I come from ruby/C# and am new to Python. I'm looking at the following
I come from a C++ background and I've been working with C# for about
I come from the asp.net world where we'd use an objectdatasource, hooked up to
I come from a C# background and I am working on a C++ project.
I come from a .NET background and need to do a web project in

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.