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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:15:31+00:00 2026-06-05T15:15:31+00:00

If I use strict mode the following code does not work. It fails on

  • 0

If I use strict mode the following code does not work. It fails on the this.bar = ‘foobar’; line. Why is this so? How can I make an object property in strict mode?

<html>
<body>
<script>
"use strict";
var foo = (function () {
    this.bar = 'foobar';
    return this;
}());
alert(foo.bar);
</script>
</body>
</html>

edit:
Thanks to James Allardice for pointing out the problem.
I was erroneously thinking that the self executing function was creating an object but it isn’t. I needed to do one of the following instead:

"use strict";
var foo = new function () {
    this.bar = 'foobar';
};
alert(foo.bar);

or (this one JSLint likes better)

"use strict";
var foo = (function () {
    var obj = {};
    obj.bar = 'foobar';
    return obj;
}());
alert(foo.bar);
  • 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-05T15:15:32+00:00Added an answer on June 5, 2026 at 3:15 pm

    In strict mode, this will not refer to the window. In your example, removing the strict mode directive will cause this to refer to window.

    Since in strict mode, this in your example is undefined, you get an error. That’s because you can’t set properties on something that is undefined.

    From MDN (emphasis on the parts relevant to your situation):

    First, the value passed as this to a function in strict mode isn’t
    boxed into an object. For a normal function, this is always an object:
    the provided object if called with an object-valued this; the value,
    boxed, if called with a Boolean, string, or number this; or the global
    object if called with an undefined or null
    this…
    Automatic boxing is a performance cost, but exposing the global object in browsers is a security hazard, because the global object provides access to functionality “secure” JavaScript environments must restrict. Thus for a strict mode function, the specified this is used unchanged

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

Sidebar

Related Questions

I have the following code: use strict; function isDefined(variable) { return (typeof (window[variable]) ===
This won't pass JSLint in strict mode: use strict; (function (w) { w.alert(w); }(window));
To enable strict mode for all JavaScript, does the use strict setting need to
http://jsbin.com/ifabem/2/edit use strict; window.x = Hello World; alert(x); // this does't throw an exception
Does executing javascript within a browser in 'strict mode' make it more performant, in
I have a couple of lines of code that work if use strict; is
What scope does the strict mode pragma have in ECMAScript5? use strict; I'd like
For more information see this Example use strict; use warnings; use CGI::Simple; use DBI;
I am doing pass-by-reference like this: use strict; use warnings; sub repl { local
Im trying to extract part of a line with perl use strict; use warnings;

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.