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

  • Home
  • SEARCH
  • 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 8412167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:36:26+00:00 2026-06-10T00:36:26+00:00

I was expecting the 2nd call of the taco function to generate a runtime

  • 0

I was expecting the 2nd call of the “taco” function to generate a runtime error since I am not calling it with the “this” keyword:

function foo() {
    var bar = "baz";

    this.taco = function() {
        console.log(bar);
    };
    this.taco();
    taco(); // I expected a runtime error here.     
}

foo();

However, it does not.

Here is a fiddle of the same code: http://jsfiddle.net/phillipkregg/gdFxU/226/

Is JavaScript using some type of implicit context management here?

Just curious, thanks!

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

    The reason is that when you call foo(), you are invoking it in the scope of the window object. That means that inside of foo(), the value of this is set to window.

    Thus, this.taco() is actually window.taco() which is the same as taco(). In other words, taco() is a global function so it works either way you call it as taco(), as window.taco() or as this.taco() when this is window.

    If you involve taco() as a new object like this where this is set to a new instance of foo and is not equal to window, then you get the expected run-time error:

    function foo() {
        var bar = "baz";
    
        this.taco = function() {
            console.log(this);
            console.log(bar);
        };
        this.taco();
        taco(); // I expected a runtime error here.     
    }
    
    var x = new foo();
    

    Example: http://jsfiddle.net/jfriend00/3LkxU/


    If you are confused about the value of this, there are these javascript rules that determine the value of this:

    1. If you call a function with new like x = new foo(), then a new instance of foo is created and the value of this is set to that object inside the foo() function and that new instance is returned from foo() by default.

    2. If you call any function normally like foo(), then the value of this is set to be the global object which in a browser is window or if in javascript’s newer “strict” mode, then this will be undefined. This is what was happening in your original example.

    3. If you call a method with an object reference like obj.foo(), then this will be set to be the obj object.

    4. If you make a function call with .apply() or .call(), then you can specifically control what the value of this is set to with the first argument to .apply() or .call(). For example: foo.call(obj) would call the foo() function and set the this pointer to the obj object.

    5. If you are not in any function call (e.g. at the global scope), then this will be either the Global object (window in a browser) or undefined in strict mode.

    6. As in all of the above rules, this is controlled by how the caller calls you, not how the function/method is defined.

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

Sidebar

Related Questions

I am expecting when I go $.each($(something).find(something), function(){ $(this).delay(1000).fadeOut(); }); then for each matching
My IDE is expecting a semicolon but I'm not sure where! It is this
I am not expecting a definite yes or no. Any knowledge you might have
I'm not sure if this is doable, but I will just give a shot.
its been sometime since i used corelated subqueries, i am not sure if i
I ran the code below expecting flow to be locked on the 2nd time
I am expecting this to return the value of 'C', but it is returning
I was expecting this code to return a 404, however it produces the output
I'm expecting this output: output:xyz But if I type the following: a = [x,
I have a class expecting a two parameters template, like this: template<template <class, class>

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.