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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:35:30+00:00 2026-05-23T09:35:30+00:00

var name = ‘The Window’; var object = { name: ‘My Object’, getNameFunc: function(){

  • 0
var name = 'The Window';
var object = {  
  name: 'My Object',
  getNameFunc: function(){    
    return function() {     
      return this.name;   
    }; 
  }
};

console.log( object.getNameFunc()() );

when i tested the code. the result is The Window. but i think this.name; should be My Object. what’s wrong with my thinking.

when i add var before the name : "My Object", it show’s an error.? why?

  • 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-23T09:35:30+00:00Added an answer on May 23, 2026 at 9:35 am

    this inside a function is the “receiver” it was invoked upon.

    That is,

    1. for the construct x.f(), this inside the function (f) will evaluate to the value of x.

    2. for all other cases, this will evaluate to window inside the invoked function. (The functions call, apply, and bind can also alter this… but that’s another story.)

    In the posted example the second function (the one with this.name) is not invoked using the x.f() form and so this is the window object.

    The “simple fix” is to use a closure: (The first function is invoked in the x.f() form and thus this is the same as object, which is as expected. We capture the value of this in the current scope via a closure created with self and the returned function.)

    getNameFunc : function () {
        var self = this
        return function () {
            return self.name
        }
    }
    

    However, I may consider another design, depending 🙂

    Happy coding.


    Additional clarification, for comment:

    …that is because you are using circle.getArea() which is of the form x.f(). Thus this inside the getArea function evaluates to circle.

    In the code posted you are invoking two different functions in a row. Imagine writing the code like this:

    var nameFunc = object.getNameFunc()
    nameFunc()
    

    The first function call is in the form of x.f() and thus this inside getNameFunc is the evaluation of object. However, in the second line, the function (nameFunc) is not invoked in the form x.f(). Therefore, the this inside nameFunc (the function returned from getNameFunc) will evaluate to window, as discussed above.

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

Sidebar

Related Questions

why this code is not working var name = $(#name).val(); var email = $(#email).val();
Given the following code: var people = new List<person>(){ new person { Name =
var name = 'Mike'; var person = { name: 'John', welcome: function(){ var name
How can I make this happen: var name = otherObject.name; //string var o =
Consider the following code: class Foo(var name: String = bar) Now i try to
$(document).ready(function(){ //global vars var name = $(#username); var email = $(#email); function usernameExists() {
I want to have a line of code similar to below: var name =
I've three strings. var ids = 1*2*3; var Name =John*Brain*Andy; var Code =A12*B32*C38; I
I have this piece of code which works in Firefox link = $('<a>link</a>'); var
I often see JavaScript code where a function may take in an options object

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.