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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:33:46+00:00 2026-05-15T07:33:46+00:00

I saw the example below explained on this site and thought both answers would

  • 0

I saw the example below explained on this site and thought both answers would be 20 and not the 10 that is returned. He wrote that both the comma and assignment returns a value, not a reference. I don’t quite understand what that means.

I understand it in relation to passing variables into functions or methods i.e primitive types are passed in by value and objects by reference but I’m not sure how it applies in this case.

I also understand about context and the value of ‘this’ (after help from stackoverflow) but I thought in both cases I would still be invoking it as a method, foo.bar() which would mean foo is the context but it seems both result in a function call bar().

Why is that and what does it all mean?

var x = 10;
var foo = {
  x: 20,
  bar: function () {return this.x;}
};

(foo.bar = foo.bar)();//returns 10
(foo.bar, foo.bar)();//returns 10
  • 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-15T07:33:47+00:00Added an answer on May 15, 2026 at 7:33 am

    It doesn’t have to do with values vs. references, it has to do with this values (as you suspected). In JavaScript, this is set entirely by how a function is called, not where it’s defined. You set the this value in one of three ways:

    1. Call the function via an object property using property accessor notation, either dotted notation (obj.foo()) or bracketed notation (obj["foo"]()).
    2. Call the function via an object property using a with statement (really just a variant of #1, but worth calling out separately, particularly as it’s not obvious from the source code)
    3. Use the apply or call features of the function instance.

    In your examples above, you’re not doing any of those, so you end up calling the function with the default this value, the global object, and so x comes from there rather than from your foo object. Here’s another way to think about what that code is doing:

    var f = foo.bar; // Not calling it, getting a reference to it
    f();             // Calls the function with `this` referencing the global object
    

    If you don’t directly use a property to actually make the call (instead retrieving the value of the property and then making the call with that), the this handling doesn’t kick in.

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

Sidebar

Related Questions

I saw this same question for VIM and it has been something that I
I saw this question asked about C# I would like an answer for PHP.
When I look at the example of the user extensions of GWT-Ext I saw
I saw this in an answer to another question , in reference to shortcomings
I saw this quote on the question: What is a good functional language on
I saw this quote in this question : MS support is poor, except when
I saw that __VIEWSTATE field gets rendered even though I have set the EnableViewState=false
i saw a example for login form same blow code class Form_Login extends Zend_Form
In my example code below, is the counter = 0 really required, or is
I have an activity defined as below: <activity android:name=com.example.ui.HomeActivity android:alwaysRetainTaskState=true/> A strange thing is

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.