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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:22:33+00:00 2026-06-03T21:22:33+00:00

I thought I had a reasonable understanding of the this object in JavaScript. When

  • 0

I thought I had a reasonable understanding of the this object in JavaScript. When dealing with objects, callbacks, and both events and handlers, I haven’t had a problem with it since time immemorial. Now, however, all has changed.

I’ve fallen head over heels in love with JavaScript. Pure JS, that is, not jQuery, prototype.js, dojo… So naturally, I’ve taken to using closures. In some cases, though, this is catching me off guard here. Take this snippet for one:

function anyFunc(par)
{
    //console.log(par);
    console.log(this);
}

function makeClosure(func)
{
    return function(par)
    {
        return func(par);
    }
}
var close = makeClosure(anyFunc);
close('Foo');

var objWithClosure = {cls:makeClosure(anyFunc),prop:'foobar'};
objWithClosure.cls(objWithClosure.prop);

var scndObj = {prop:'Foobar2'};
scndObj.cls = makeClosure;
scndObj.cls = scndObj.cls(anyFunc);
scndObj.cls(scndObj.prop);

In all three cases, this logs as the window object. It’s an easy fix, of course:

function makeClosure(func)
{
    return function(par)
    {
        return func.call(this,par);
    }
}

This fix works, I put it here to avoid people answering this, without explaining what I need to know: why is this behaving the way it does here?

ensures the caller is effectively the object that the closure belongs to. What I fail to understand is this:
Sure enough, this points to the window object in the first case, but in other cases, it shouldn’t. I tried logging this in the makeClosure function just before returning, and it did log the object itself, not the window object. But when the actual closure is used, this is back to pointing to the window object. Why?

The only thing I can think of is that, by passing the anyFunc function as an argument, I’m actually passing window.anyFunc. So I tried this quick fix:

function makeClosure(func)
{
    var theFunc = func;
    return function(par)
    {
        theFunc(par);
    }
}

With the expected results, this now points to the objects, but again: Why? I have a few idea’s (theFunc is a reference to the function in the local scope [this > private: theFunc]?), but I’m sure there are people here with a lot more know-how when it comes to JS, so I was hoping to get some more explanation or links to articles worth reading from them…

Thanks

Update

Here’s a fiddle, may be I left something out, but here this logs all sorts of things 😉

Edit/Update 2

The case that confuses me is here.

Final Edit

Ok, This is getting a rather messy post. So to clarify: What I was expecting was a behaviour similar to this:

function makeClosure()
{
    function fromThisFunc()
    {
        console.log(this);
    }
    return fromThisFunc;
}

var windowContext = makeClosure();
windowContext();
var objectContext = {cls:makeClosure()};
objectContext.cls();

What caught me, was that the function anyFunc wasn’t declared within the correct scope, and therefore, this pointed to the window object. I found this out by reading an ancient scroll I found somewhere on the web.

But something a little more complicated has happened because the function object now referred to by globalVar was created with a [[scope]] property referring to a scope chain containing the Activation/Variable object belonging to the execution context in which it was created (and the global object). Now the Activation/Variable object cannot be garbage collected either as the execution of the function object referred to by globalVar will need to add the whole scope chain from its [[scope]] property to the scope of the execution context created for each call to it.

So what I needed to do, was simplify rather then complicate things:

function fromThisFunc()
{
    console.log(this);
}

function makeClosure(funcRef)
{
    //some code here
    return funcRef;
}

That should work, right?

PS: I’ll except Alnitak’s answer, but special thanks goes to Felix Kling for all the patience and info.

  • 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-03T21:22:34+00:00Added an answer on June 3, 2026 at 9:22 pm

    As soon as you call:

    return func(par);
    

    You’re creating a new scope (with its own this) and in this case because you haven’t specified an object, this === window usually or undefined in strict mode. The called function does not inherit whatever this was in the calling scope.

    Ways to set a value for this are:

    myobj.func(par);  // this === myobj
    

    or

    func.call(myobj, ...)  // this === myobj
    

    There are also:

    • apply
    • bind
    • arrow functions, where this is set to the same value as the outer execution context (also see MDN:Arrow functions )
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought I had reasonable answers for this question at a recent interview, but
I thought I had understanding of this in my previous question about linked lists,
I thought I had this figured out but it turns out I'm just deleting
I thought I had a good understanding of how quicksort works, until I watched
Alright, I thought I had this whole setTimeout thing perfect but I seem to
So, thought I had this working last night, couldve sworn it. Now it no
can anyone help, i have problem doing a sort, I thought i had it
I thought I had correctly followed the recommendations in this question for checking undefined
I thought I had this perfectly working but apparently not. I left my project
So I thought I had a good basic understanding of exception-handling in Java, but

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.