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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:03:06+00:00 2026-06-12T09:03:06+00:00

I know how to get around this particular problem, but I would like to

  • 0

I know how to get around this particular problem, but I would like to know why it happens. Basically, when I try to invoke a function like this:

(callFoo ? this.foo : this.bar)();

It calls the correct foo function, but inside of foo, this is the global, window object instead of the object I expect.

I would expect that this does the same thing but it does not:

(this.foo)();

The code above calls the right function and maintains the correct context (this is what I expect it to be).

Here is a jsfiddle for you to play around with.

Could someone please explain what is going on? I understand how to get around the problem (I’m not even a fan of that syntax), but I still want to know why this becomes the window if you return a function from a ternary operator.


EDIT
I’d like to refine my question:
It makes sense to me that this:

(callFoo ? this.foo : this.bar)();

is equivalent to:

var f = (callFoo ? this.foo : this.bar);
f();

And it makes sense to me why this becomes the window within that function.

Why doesn’t the same thing happen here:

(this.foo)();
  • 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-12T09:03:07+00:00Added an answer on June 12, 2026 at 9:03 am

    Reason for the discrepancy:

    var obj = new (function MyConstructor(){
        this.getConstructor = function(){ return this.constructor.name; }
    });
    

    When operands are operated on by any operator, the results work much like the return value of a function. An object method passed is no longer treated like it’s tied to the object.

    (function(){ return obj.getConstructor; })(); //'Window'
    

    But in the case where nothing is happening inside the parens other than property access, the parens are simply ignored rather than treated as operators themselves. So:

    (obj.getConstructor)(); //'MyConstructor'
    

    Is really just equivalent to:

    obj.getConstructor();
    

    But add a valid operation of any kind resulting in the method:

    (false || obj.getConstructor)(); //'Window'
    

    And obj.getConstructor is treated as a method that’s been passed rather than a method tied to an object via the ‘.’ association.

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

Sidebar

Related Questions

I can't quite get my head around this particular problem, and I'm hoping you
I know how to get elements with a particular attribute : $("#para [attr_all]") But
I am using Nokogiri to analyze some HTML, but, I don't know how get
I know how to get a current date. but I want it to display
I know how to get a string from another string, but only if it's
I know I can get touch events in my iPhone app. But these touch
I know how to get the user that last modified a file, but does
I know you can get the SQL, but it's paramterized without the parameters being
I know how to get paths and urls no problem (eg. new_color_path for the
I have been trying to wrap my head around why this is happening but

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.