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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:04:40+00:00 2026-06-16T16:04:40+00:00

Functions nested under a prototype function do not get the this.variables defined for the

  • 0

Functions nested under a prototype function do not get the this.variables defined for the prototype.

var Person, p;

Person = function(name, age) {
  this.name = name;
  this.age = age;
};

Person.prototype.getInfo = function() {
  var innerFun;
  innerFun = function() {
    return this.name;
  };
  return "name: " + (innerFun()) + " age: " + this.age;
};

p = new Person('dork', 99);

console.log(p.getInfo());      // name:  age: 99

I thought that since every function is an object, this would be different within every function definition; but the following code blows away that logic.

var getInfo;

getInfo = function() {
  var display;
  this.name = 'dork';
  return display = function() {
    return this.name;
  };
};

console.log(getInfo()());     // dork

Is there a logic behind this behaviour or should I just take it as a rule and use call() to get around this 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-06-16T16:04:42+00:00Added an answer on June 16, 2026 at 4:04 pm

    Short version of this behavior:

    1. In function invoked as x.f(), this will be x.
      (Note: invoked as x.f(). It does not matter how and where you define it)
    2. In function invoked as f(), this will be window.
    3. In function invoked as x['f'](), this will be window x (not sure why I thought otherwise)
    4. In function invoked as f.call(x) or f.apply(x), this will be x.

    Again, the prototype does not matter, if you do

    var f = p.getInfo;
    console.log(f());
    

    you will see that only calling style matters.

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

Sidebar

Related Questions

I looked at this: Returning values from nested functions in Javascript but it did
I'm trying to get an average of sums using nested aggregate functions and grouping.
This Javascript code is using the 'this' keyword inside a nested function (which is
I am trying to work out why I can not access the nested function
I've used this useful JQuery function that serializes nested elements. the problem is how
I know that nested functions are not part of the standard C, but since
I've got a few MouseEvent listeners outside of a function that contains nested functions
If I have nested functions in C#, how to exit from both function at
Is this a a pythonic implementation? I'm calling nested functions dynamically from a string
Im Displaying This Error: nested functions are disabled, use -fnested-functions to re-enable ,Im running

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.