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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:10:48+00:00 2026-06-17T09:10:48+00:00

I am experimenting with the context of ‘this’ in Javascript and I have a

  • 0

I am experimenting with the context of ‘this’ in Javascript and I have a situation that I don’t understand.

Based on the way javascript works found from here, I understand that when a function is called on an object, the object is implicitly passed in as the firest parameter (or explicitly when using the call method.

But there are 2 cases that I tried to test that didn’t do what I expected. Please look at the 2 lines after //Why doesn’t ths work? Why are the follwoing 2 values undefined?

Here is the code in a jsFiddle (also pasted below)

function Beta(c, myValParam) {
  var callback = c;
  this.myVal = myValParam;
  this.RunCallback = function () {
   callback();
  }

  this.ShowVal = function () {
    alert("FROM Beta: " + this.myVal);
  }
}

function Alpha() {
  this.myVal = "Alpha's Property";
  this.ShowVal = function () {
    alert("FROM Alpha: " + this.myVal);
  }
}
a = new Alpha();
a.ShowVal();

b = new Beta(a.ShowVal, "Beta's property passed in");
b.ShowVal();

//Why doesn't ths work? Why are the follwoing 2 values undefined?
b.RunCallback.call(b); //Shouldn't this display the value in b?
b.RunCallback();

b2 = new Beta(function() { return a.ShowVal.call(a); }, "Z");
b2.RunCallback();

EDIT: Thanks to the answers from Quentin, dystroy and dough, I’ve updated the jsFiddle to show the values produced when the context reverts to the window object

And here is the code with the call to callback.call(this) which fixes the problem I was having

  • 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-17T09:10:48+00:00Added an answer on June 17, 2026 at 9:10 am

    Shouldn’t this display the value in b?

    You are calling (in the context of b) a function which does nothing with this. That function calls callback (which is a copy of a.showVal) in the context of window (the default object).

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

Sidebar

Related Questions

today i was thinking about tell! don't ask! and experimenting with this code. interfaces:
I'm experimenting with MVVM in the context of a simple WPF app. I have
Context: Started project on GitHub and have been experimenting with git commands. The history
Context: I have an NSArrayController tied to Core Data that supplies rows for an
I'm experimenting with Android AccountManager . I have an Account authentication service that shows
When experimenting with (embedded) Apache Derby DB, I noticed that a fresh database, with
I am experimenting with Dart. I have created an on click event like so:
I'm experimenting with a visual studio addon and have come across very odd behaviour.
I am experimenting with the Twitter API for Python and have run into a
I've been experimenting with extending Application as a quick way of getting hold of

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.