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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:17:17+00:00 2026-05-18T23:17:17+00:00

Okay, I’m hating Javascript right now, and I hope someone can help me. I

  • 0

Okay, I’m hating Javascript right now, and I hope someone can help me.

I have code which is set up like the following:

function Obj1() {
    var me = this;

    this.something = "yay";

    this.getThis = function(){
        return me;
    }
}

Obj1.prototype.method = function() {
    return this.something;
};

function Obj2() {
    this.something = "nay";

}
Obj2.prototype.method = function() {
    return this.something;
}; 



var o1 = new Obj1();
var o2 = new Obj2();

document.write(o1.method()); // Returns yay
document.write(o1.method.call(o2));   // Returns nay, but I need "yay" here

(JSFiddle @ http://jsfiddle.net/A9u9K/)

My Problem is, that I need to call Obj1.method in the second case, but I am absolutely unable to get a reference to the object 🙁

How can I work around this?

Edit: Sorry, I got my example code pretty wrong 🙁 Updated it. I took most of the code from a previous answer, because it is much nicer and still illustrates my problem.

  • 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-18T23:17:18+00:00Added an answer on May 18, 2026 at 11:17 pm

    Updated Answer:

    document.write(o1.method.call(o2)); // Returns nay, but I need "yay" here

    You’ve said you’ve got it sorted now, but as the answer to that isn’t actually shown here on SO, I figured I may as well update to show it.

    If it’s method you want to have access me, even if it’s been called with a different this value, you have to define it like getThis, as a closure over me:

    function Obj1() {
        var me = this;
    
        this.something = "yay";
    
        this.method = function() {
            return me.something;
        };
    
        this.getThis = function(){
            return me;
        };
    }
    
    function Obj2() {
        this.something = "nay";
    
    }
    Obj2.prototype.method = function() {
        return this.something;
    };
    

    …or of course, if you don’t need the “something” to be a property on the object, just make it a var within the constructor (a private variable, like me):

    function Obj1() {
        var me = this;
        var something = "yay";
    
        this.method = function() {
            return something;
        };
    
        this.getThis = function(){
            return me;
        };
    }
    
    function Obj2() {
        this.something = "nay";
    
    }
    Obj2.prototype.method = function() {
        return this.something;
    };
    

    Original Answer: (To Revision 1 of the question, which didn’t have me.)

    but I thought that, when creating a closure (as I do in 4) Javascript should preserve “this”.

    this is set entirely by how a function is called, not where it’s defined; more about that here and here. But the way you’ve defined your getThis function, you can use the fact it closes over the constructor call to solve this (no pun) without using this:

    function Obj1() {
        var me = this;               // <== Use a variable to remember `this`
    
        this.something = "yay";
    
        this.method = function() {
            return this.something;
        };
    
        this.getThis = function(){
            return me;               // <== Return it
        };
    }
    

    Live example

    More about closures and the plumbing that makes the me thing work here.

    There is a cost involved in this, and just generally in your pattern of defining functions within the constructor function: Each individual object created by Obj1 and Obj2 gets its own copy of each function. This can have memory implications if there are lots of these objects running around (but unless you have lots, you needn’t worry and you get benefits like the me thing and other private variables). In constrast, if you use a function assigned to the prototype, all instances will share a single, common copy of the function.

    In your sample code, only the getThis function really needs to be duplicated for every instance (because you’re relying on the closure), so you can do this to avoid unnecessary function proliferation:

    function Obj1() {
        var me = this;
    
        this.something = "yay";
    
        this.getThis = function(){
            return me;
        };
    }
    Obj1.prototype.method = function() {
        return this.something;
    };
    
    function Obj2() {
        this.something = "nay";
    
    }
    Obj2.prototype.method = function() {
        return this.something;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I'm making a table right now for Box Items. Now, a Box
Okay, I've been struggling with this for a while now. I have a standard
Okay, so I've got some C code to perform a mathematical operation which could,
Okay, here's the scenario. I have a utility that processes tons of records, and
Okay, I've seen but haven't programmed in C# before. You can assume I'm competent
Okay. I know this looks like the typical Why didn't he just Google it
Okay, I have read, and tried a lot of things on how to implement
Okay this is the css code I put on the master page so it
Okay, I've looked all over the internet for a good solution to get PHP
Okay, so I'm running a small test webserver on my private network. I've got

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.