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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:32:00+00:00 2026-06-05T13:32:00+00:00

in the following example, to send as parameter to the method lostThis object instObj,

  • 0

in the following example, to send as parameter to the method “lostThis” object “instObj”, “this” is the window object.

var obj = function() {};
obj.prototype.lostThis = function() {
    console.log('lostThis', this instanceof obj, this);
};

var instObj = new obj;

var caller = {
    runFn: function(fn) {
        fn();
    }
};

caller.runFn(instObj.lostThis);

Console response:

lostThis false Window

run example

In the following example (slightly more complex) there are different ways to call the methods of “instObj” where it is the same and others where I can keep the “this” object.

var obj = function() {};

obj.prototype.methodRefHasThis = function() {
    var t = this;
    return function() {
        console.log('methodRefHasThis ', t instanceof obj, t);
    };
};

obj.prototype.methodRefLostThis = function() {
    console.log('methodRefLostThis ', this instanceof obj, this);
};

obj.prototype.methodRefMaybeThis = function() {
    console.log('methodRefMaybeThis ', this instanceof obj, this);
};

var instObj = new obj;
var caller = {
    runFn: function(fn) {
        fn();
    }
};

// width jQuery
$('button')
    .bind('click', instObj.methodRefHasThis())
    .bind('click', instObj.methodRefLostThis);

caller.runFn(instObj.methodRefHasThis());
caller.runFn(instObj.methodRefLostThis);
caller.runFn(function() {
    instObj.methodRefMaybeThis();
});​

Console response:

methodRefHasThis  true obj
methodRefLostThis  false Window
methodRefMaybeThis  true obj

methodRefHasThis  true obj
methodRefLostThis  false <button>​press here​</button>​

run example

I understand that this happens with jQuery to assign the method to an event, but could I call the method “methodRefLostThis” no lose “this” object to be passed by reference?

thanks

Solution by @am_not_i_am , @Dan_Davies_Brackett and @Ben_Lee

var obj = function() {};
obj.prototype.lostThis = function() {
    console.log('lostThis', this instanceof obj, this);
};

var instObj = new obj;

var caller = {
    runFn: function(fn) {
        fn();
    }
};

caller.runFn(instObj.lostThis.bind(instObj));
caller.runFn($.proxy(instObj.lostThis, instObj));

Console response:

lostThis true obj
lostThis true obj

 ​

run example

  • 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-05T13:32:01+00:00Added an answer on June 5, 2026 at 1:32 pm

    If you don’t want to use one of the techniques that you’ve found to work, you can use Function.prototype.bind to bind the calling context to a new function…

    caller.runFn(instObj.lostThis.bind(instObj));
    

    This returns a new function that when invoked, will set the calling context to whatever you passed as the first argument to .bind().

    Any additional arguments passed to .bind() will be set as fixed arguments to the returned function.

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

Sidebar

Related Questions

I have following condition: How do I send email to example@mail.com on submitButton clicklistener
In the following example i can create an object dynamically via a string; however,
Say for example this is my C ( & Objective-C ) method as follows.
Suppose you have a simple block of code like this: app.get('/', function(req, res){ res.send('Hello
How can I pass a request parameter to fr-workflow-send-submission in persistence-model.xml? For example if
This is the following code example from Twisted for dealing with receiving of multicasts.
The following example will not compile for me: #include <iostream> #include <functional> #include <string>
Example: Suppose in the following example I want to match strings that do not
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
Take the following example, I have a class public class SomeItem { public string

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.