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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:34:10+00:00 2026-05-23T21:34:10+00:00

I have code like this: function demo() { this.val=5; function() { this.val=7; }(); }

  • 0

I have code like this:

function demo() {
    this.val=5;
    function() {
        this.val=7;
    }();
}

Now when I give execute this code in the firefox or chrome console it gives a syntax error. I don’t understand why this is an error because I have read that javascript functions are objects so when I call the anonymous function, inside it this points to function demo and should change the val to 7, so if I do

var x=new demo();
x.val;   //should give 7

but when I do this

function demo() {
    this.val=5;
    var f=function() {
            this.val=7;
    }();
}
window.val;    // gives 7

I don’t understand if functions are objects then why the this in the anonymous function points to window and not demo.
Please explain this.

  • 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-23T21:34:11+00:00Added an answer on May 23, 2026 at 9:34 pm

    i dont understand why this is an error

    because

    function() {
        this.val=7;
    }();
    

    is evaluated as function declaration and function declarations need a name. To make it interpreted as function expression, you need to put it in parenthesis:

     (function() {
        this.val=7;
     }());
    

    (making it an immediate function)

    or assign the function to a variable:

    var f = function() {....};
    f();
    

    What you do in the second snippet is a mixture of both and although being valid, it does not make much sense. f will have the value undefined.

    i dont understand if functions are objects then why the this in the anonymous function points to window and not demo.

    Functions don’t inherit the context they are called in. Every function has it’s own this and what this refers to is determined by how the function is called. It basically boils down to:

    • “Standalone” ( func()) or as immediate function ( (function(){...}()) ) : this will refer to the global object (window in browsers)

    • As property of an object ( obj.func()): this will refer to obj

    • With the new [docs] keyword ( new Func() ): this refers to an empty object that inherits from Func.prototype

    • apply [docs] and call [docs] methods ( func.apply(someObj) ): this refers to someObj


    Further reading:

    • MDC JavaScript Guide – Functions
    • MDC JavaScript Reference – Function and functions scope
    • ECMAScript 5, Section 13 – Function Definition
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code like this var MyObj = { f1 : function(o){ o.onmousedown =
If I have a chunk of code like this: .hover( function () { hoverState($(#navbar
When we have code like: function a(){ var x =0; this.add=function(){ alert(x++); } }
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
I have code like this: class ToBeTested { function simpleMethod($param) { if(0 === $param)
I have code that looks like this: function foobar(array& $objects, $con = null) {
If I have code like this: <script> function determine() { // ???? } </script>
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this: template <typename T, typename U> struct MyStruct { T
I have code like this to move the player in my game left, right,

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.