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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:20:12+00:00 2026-05-31T23:20:12+00:00

Why is this in an anonymous function undefined when using javascript in strict mode?

  • 0

Why is this in an anonymous function undefined when using javascript in strict mode? I understand why this could make sense, but I couldn’t find any concrete answer.

Example:

(function () {
    "use strict";

    this.foo = "bar"; // *this* is undefined, why?
}());

Test in a fiddle: http://jsfiddle.net/Pyr5g/1/
Check out the logger (firebug).

  • 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-31T23:20:14+00:00Added an answer on May 31, 2026 at 11:20 pm

    It’s because, until ECMAscript 262 edition 5, there was a big confusion if people who where using the constructor pattern, forgot to use the new keyword. If you forgot to use new when calling a constructor function in ES3, this referenced the global object (window in a browser) and you would clobber the global object with variables.

    That was terrible behavior and so people at ECMA decided, just to set this to undefined.

    Example:

    function myConstructor() {
        this.a = 'foo';
        this.b = 'bar';
    }
    
    myInstance     = new myConstructor(); // all cool, all fine. a and b were created in a new local object
    myBadInstance  = myConstructor(); // oh my gosh, we just created a, and b on the window object
    

    The last line would throw an error in ES5 strict

    "TypeError: this is undefined"
    

    (which is a much better behavior)

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

Sidebar

Related Questions

I'm specifically referring to JavaScript anonymous function but this could be relevant to other
We have this anonymous function in our code, which is part of the jQuery's
This produces an anonymous function, as you would expect (f is a function with
When I pass 'this' to an anonymous function like so: MyClass.prototype.trigger = function(){ window.setTimeout(function(){this.onTimeout();},1000);
Is there a way I can get this array walk with my anonymous function
In this SO thread, Brian Postow suggested a solution involving fake anonymous functions: make
Why are 'me' and 'this' undefined when setTimeout calls it's anonymous callback? var gMyObj
Long story short: var o="before"; x = function() //this needs to be an anonymous
I am creating a object in javascript like this function myobject() { this.myvar1 =
How can I get this anonymous function to return object.Property as a string? ()

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.