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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:28:21+00:00 2026-05-26T19:28:21+00:00

When I run this code: var Test = function() { return this.stuff; }; Test.stuff

  • 0

When I run this code:

var Test = function() {
    return this.stuff;
};

Test.stuff = 'Neat!';

document.write(Test() || 'Not neat.');

Why do I get ‘Not neat.’? Why can’t I access the stuff property using this.stuff?

  • 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-26T19:28:22+00:00Added an answer on May 26, 2026 at 7:28 pm

    While other people have posted why this occurs (the understanding of this is incorrect), here is one solution which will work reliably.

    Update: As Raynos noted, when using strict mode functions in ECMAScript 5th Edition, it is invalid to use arguments.callee (it will throw a TypeError). Thus caution should be exercised if using this approach. (When using a [correct] ECMAScript 5th edition engine, there is no reason to use arguments.callee over the name given to the function which is bound to the new scope — see the end of the answer.)

    var Test = function() {
       // arguments.callee is the current function, if any
       return arguments.callee.stuff
    }
    Test.stuff = 'Neat!'
    alert(Test() || 'Not neat.') // Neat!
    

    Another is to use a closure:

    var Test = (function () {
      function fn () {
        // closure over fn, which names this function-object
        return fn.stuff
      }
      fn.stuff = 'Neat!' // here
      return fn          // do not combine with function declaration!
    })()
    Test.stuff = 'Neat!' // or here
    alert(Test() || 'Not neat.') // Neat!
    

    Or, a closure over a variable directly:

    var Test = (function () {
      var stuff = 'Neat!'
      return function () {
        // variable closure, no property
        return stuff
      }
    })()
    alert(Test() || 'Not neat.') // Neat!
    

    Or… so many ways.

    Happy coding.


    Another approach that was pointed out by Aadit M Shah is to use the function identifier to refer to the current function:

    var Test = function Temp () {
       return Temp.stuff
    }
    Test.stuff = 'Neat!'
    alert(Test() || 'Not neat.') // Neat! (But see below.)
    

    As Aadit points out, this is valid, as per the ECMAScript 5th edition specification, page 99:

    The Identifier in a FunctionExpression can be referenced from inside the FunctionExpression’s FunctionBody to
    allow the function to call itself recursively.
    However, unlike in a FunctionDeclaration, the Identifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.

    However, some browsers (at least IE9) implements this incorrectly (and I am not sure if the above noted behavior is well-defined in the 3rd edition). Consider:

    var x = function y () { return y }; y = 42; x();
    

    In IE9 it will yield 42 and in FF8 it will yield the function-object. IE9 is incorrect here as it introduces y as a variable in the enclosing scope, which is forbidden by ECMAScript for function expressions. Here is an in-context example of how this incorrect implementation can lead to different results:

    var Test = function Temp () {
       return Temp.stuff
    }
    Test.stuff = "Neat!"
    Temp = {}
    alert(Test() || 'Not neat.') // 'Not neat.' in IE9, 'Neat!' in FF8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Examine this code var _class=function() { this.Test=100; this.Callback=function(msg) { alert(msg+\r\n+this.Test); } } function run(call)
On document ready I run this code: jQuery(document).ready(function(){ jQuery('#button').click(function() { jQuery('#contact_form').load(/Users/mge/Downloads/jquery-ajax-1/readme.txt); return false; });
When I run this code the selected item is not visible. I've already tried
A JavaScript newbie here. I have this following code: function testObject(elem) { this.test =
I can run this code in Android app (using PhoneGap adn jQuery Mobile) but
I'm getting an error of buildXML is not defined when I run this code:
Why this code don't work,when i want run this code vwd 2008 express show
when i run this code : CONADefinitions.CONAPI_FOLDER_INFO2 FolderInfo; int iResult = 0; IntPtr Buffer
When I run this code, About half-way through the concatenation loop, $xml becomes null
I'm trying to run this code: let coins = [50, 25, 10, 5, 2,1]

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.