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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:34:58+00:00 2026-06-13T00:34:58+00:00

Let see code first var name = The Window; var object = { name

  • 0

Let see code first

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

alert(object.getNameFunc()());

the result is "The Window";
I want to know what happend in every step.
I think this is point to the object which call this function; right?
But why in this case this is window

  • 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-13T00:35:00+00:00Added an answer on June 13, 2026 at 12:35 am

    The second function invocation doesn’t take place from the context of an object, but rather from the function returned from the first invocation.

    Because there’s no object context, the this value becomes the default window just like any other function.

     //   v-----------------v------function has context
    alert(object.getNameFunc()());
                // -----------^----second function was returned from the first
                //                   and invoked so there's no object context
    

    If we instead assigned the returned function to object, then invoked it from that context, this would then be a reference to object.

    obj.foo = object.getNameFunc();
    obj.foo();  // "My Object"
    

    Same exact function, but now it’s being invoked from as a property of object, which implicitly sets its this value to object.


    The rules of this are really pretty simple and easy to understand, but may not be what you’d expect at first.

    The this value is very dynamic, and is based entirely on how a function is invoked.

    foo();            // 'this' is 'window'
    
    object.foo();     // 'this' is 'object'
    
    foo.call(object); // 'this' is 'object'
    foo.apply(object);// 'this' is 'object'
    
    var bar = foo.bind(object);
    bar();            // 'this' is 'object'
    

    So you can see that the default is window when the function is invoked without any sort of connection to another object.

    But when we call the function as a property of an object, this suddenly refers to that object.

    Or we can use .call, .apply or .bind to manually set the this value of the function call.

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

Sidebar

Related Questions

First , let's see the code. var a=0; b=1; document.write(a); function run(){ document.write(b); var
Let's see I want to do this, i want to get the parent of
First let me say that I did see this article: How to remove AspxAutoDetectCookieSupport
I'll let you see the code first then tell you what my problem is:
First, let's see the code: //The encoding of utf8.txt is UTF-8 StreamReader reader =
Let me show the code first. WCF servicecontract function: public List<VenueData> GetVenues() { List<VenueData>
Sometimes I see code like let (alt : recognizer -> recognizer -> recognizer) =
Recently I met a strange problem, see code snips as below: var sqlCommand: string;
First , I want to thank all the persons who works for this site,
Let's say I want to have a function which reads data from the SerialPort

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.