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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:16:09+00:00 2026-06-13T02:16:09+00:00

Can you explain me why does the second call of fn gives an error?

  • 0

Can you explain me why does the second call of fn gives an error? The code is below.

function Test(n) {
  this.test = n;

  var bob = function (n) {
      this.test = n;
  };

  this.fn = function (n) {
    bob(n);
    console.log(this.test);
  };
}

var test = new Test(5);

test.fn(1); // returns 5
test.fn(2); // returns TypeError: 'undefined' is not a function

Here’s a JSfiddle that reproduces the error http://jsfiddle.net/KjkQ2/

  • 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-13T02:16:10+00:00Added an answer on June 13, 2026 at 2:16 am

    Your bob function is called from the global scope. Thefore, this.test is pointing at a global variable named test which is overwriting the variable you created. If you run console.log(window.test), you’ll what’s happening.

    For your code to behave as intended, you would need one of the following

    function Test(n) {
      this.test = n;
    
      // If a function needs 'this' it should be attached to 'this'       
      this.bob = function (n) {
          this.test = n;
      };
    
      this.fn = function (n) {
        // and called with this.functionName
        this.bob(n);
        console.log(this.test);
      };
    }
    

    OR

    function Test(n) {
      this.test = n;
    
      var bob = function (n) {
          this.test = n;
      };
    
      this.fn = function (n) {
        // Make sure you call bob with the right 'this'
        bob.call(this, n);
        console.log(this.test);
      };
    }
    

    OR closure based objects

    // Just use closures instead of relying on this
    function Test(n) {
      var test = n;
    
      var bob = function (n) {
          test = n;
      };
    
      this.fn = function (n) {
        bob(n);
        console.log(test);
      };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody explain what does JdClient is in this code: public JdClient client =
Can somebody explain to me what this does in javascript? (function (x,y){}(x,y)); or this
Can anyone explain what the following PHP Code does function query($query_string) { if ($query_string
Can someone explain the second line of this function? is it setting two variables
can somebody explain how does glutMainLoop work? and second question, why glClearColor(0.0f, 0.0f, 1.0f,
Anyone can explain in details what the following macro does? #define write_XDATA(address,value) (((char *)0x010000)
does anyone knows any good resources and articles that can explain how to create
Can somebody explain what ASPXAuth cookie does? My website uses forms auth and I
Can someone explain what the following does? private HashSet nodes[]; nodes = new HashSet[21];
Can someone explain what the 'e' flag does, or link me to somewhere that

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.