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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:14:33+00:00 2026-06-13T17:14:33+00:00

Regarding this code: var name = Jaguar; var car = { name:Ferrari, getName:function(){ return

  • 0

Regarding this code:

var name = "Jaguar";
var car = {
  name:"Ferrari",
  getName:function(){
    return this.name;
  }
};

alert((car.getName = car.getName)());

The output is: Jaguar.

Why does this object correspond to Window and not the object contained in the car variable?

It seems that the fact to reassign the object’s function to itself leads to lose the assignment of this to the object when the function is called…

I’m trying to guess: Does it exist a kind of mechanism (using variable or other) that keep an eye on the non-reassignement of an object’s function so that if that situation happens, this mechanism would prevent the assignement of the this keyword as usual (as being equals to the object)?

  • 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-13T17:14:35+00:00Added an answer on June 13, 2026 at 5:14 pm

    The reason is fairly subtle: this in JavaScript is determined entirely by how a function is called. To have this set to car during the call to getName, you have to call getName immediately upon retrieving it from the car object, like this:

    car.getName() // or
    car["getName"]()
    

    (Or via Function#call or Function#apply, which let you specify the value for this explicitly.)

    What you’re doing in your example is effectively this:

    // Set f to the result of the assignment expression,
    // which is a reference to the getName function
    var f = (car.getName = car.getName);
    
    // Call it (separately)
    f();
    

    …which is different. Functions called in that way get this set to the global object (window, in browsers). (Except in strict mode; in strict mode this would be undefined.)

    More (from my anemic blog):

    • Mythical methods
    • You must remember this

    Does it exist a kind of mechanism (using variable or other) that keep an eye on the non-reassignement of an object’s function so that if that situation happens, this mechanism would prevent the assignement of the this keyword as usual (as being equals to the object)?

    I’m not entirely sure I follow that question, but if you want to have a function that always has a preset this value, then yes, there are a couple of ways to do that.

    One is to use the new ES5 function bind:

    var name = "Jaguar";
    var car = {
      name: "Ferrari"
    };
    car.getName = function(){
      return this.name;
    }.bind(car);
    alert((car.getName = car.getName)()); // "Ferrari"
    

    bind returns a function that always has this set to the argument you give it.

    The other way is to use a closure. And in fact, you can create a bind-like function in ES3 very easily:

    function pseudoBind(func, thisArg) {
        return function() {
            return func.apply(thisArg, arguments);
        };
    }
    

    That doesn’t do everything bind does, but it does the this part. Then you’d have:

    var name = "Jaguar";
    var car = {
      name: "Ferrari"
    };
    car.getName = pseudoBind(function(){
      return this.name;
    }, car);
    alert((car.getName = car.getName)()); // "Ferrari"
    

    More on closures (again from the blog):

    • Closures are not complicated

    In a future spec, we’ll be getting a declarative way of creating functions that have a preset this value (so-called “arrow functions” because the syntax for them involves uses => rather than the function keyword).

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

Sidebar

Related Questions

What the heck is up with this code? $(.submit).click(function(){ alert(clicked); var name = $(.name).val();
I have a cross-language problem regarding md5 :). I have this code in Java:
I asked a question on Javascript this points to Window object regarding this points
I'm reading this code sample : And since I don't know C#, I decided
i was reading this code, where setRegions is called after RootViewController is released :
Note: This code actually codes from a tutorial book I'm reading at the moment,
i am reading an excel file using this code i found over the net:
I am reading On lisp and encountered this code (I simplified it a bit).
I'm learning by reading this tutorial: Link Here's the code: <?php require_once 'Zend/Loader.php'; class
I was reading a blog where the writer said this Code doesn’t exist unless

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.