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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:25:36+00:00 2026-05-16T03:25:36+00:00

A JavaScript newbie here. I have this following code: function testObject(elem) { this.test =

  • 0

A JavaScript newbie here. I have this following code:

function testObject(elem) {
    this.test = "hi";
    this.val = elem;
    console.log(this.test+this.val);
    echo();

    function echo () {
        console.log(this.test+this.val);
    }
}

var obj = new testObject("hello");

When it is run, I expect “hihello” to be outputted twice in the console. Instead it outputs as expected the first time but returns NaN the second time.

I’m sure I’m missing something here. I thought that the internal function can access the vars held outside. Can someone please guide me? I’m more of a functional UI developer and don’t have much experience with OO code.

Thanks!

  • 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-16T03:25:37+00:00Added an answer on May 16, 2026 at 3:25 am

    The problem is that inside echo the this value points to the global object, and this.test and this.val (which are referring to window.test and window.val) are undefined.

    You can set the this value of echo by invoking it like:

    echo.call(this);
    

    That happens because you were invoking the function by echo();, then the this value is implicitly set to the global object.

    Give a look to this question to learn how the this value works.

    Edit: For being able to calling just echo(); you should persist the this value from the outer function context, there are a lot of ways to do it, for example:

    //...
    var instance = this; // save the outer `this` value
    function echo (){
      console.log(instance.test+instance.val); // use it
    }
    echo();
    //...
    

    Or

    //...
    var echo = (function (instance) {
      return function () {
        console.log(instance.test+instance.val);
      };
    })(this); // pass the outer `this` value
    echo();
    //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bit of a JavaScript newbie here - I am firing this basic bit of
Super newbie at Javascript here. I have a problem with whitespace that I'm hoping
JavaScript newbie here, I was going through some js code at work when i
Javascript newbie here, trying to load Twitter profile images using the URLs returned by
Bit of a javascript newbie so not sure if this question is easy or
Im a javascript newbie and would need some help with this simple thing... I
I am a newbie at Javascript and Jquery. I have a form that I
I am newbie. many of javascript code start with <!-- <script type=text/javascript> <!-- and
So I'm a complete newbie and stuck on a conditional statement. Here's my code:
i'm a total newbie to java and Alfresco and i have this simple problem:

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.