最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the context of an anonymous function? - Stack Overflow

programmeradmin0浏览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.

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.

Share Improve this question edited Jun 13, 2015 at 14:32 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 22, 2011 at 21:54 loveshlovesh 5,40110 gold badges64 silver badges96 bronze badges 3
  • That's just the way it works. If you call a function "normally", the this keyword will point to the global object... – Šime Vidas Commented Jul 22, 2011 at 21:57
  • @Šime Vidas what is "normally" here? and are functions not objects? – lovesh Commented Jul 22, 2011 at 21:58
  • "normally" = by appending parens to the name of the function, for instance: foo(). The other ways of calling a function are (1) like a constructor and (2) via call/apply. In those cases, the rules for the this keyword are different. Yes, functions are objects. – Šime Vidas Commented Jul 22, 2011 at 22:01
Add a comment  | 

2 Answers 2

Reset to default 21

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

You can do what you described like this:

function demo() {
    var self=this;

    this.val = 5;

    var f = (function() {
        self.val = 7;
    })();
}
发布评论

评论列表(0)

  1. 暂无评论