2

I have found different behavior of code when function is called / returned in different ways. It is quite confusing. So to clear my doubts I have stripped down the whole code to minumum:

Let us consider a simple function:

function f()
{
   //some code
}

var objf = f();

Q1. Now objf is undefined. Why? What does f() returns?

function f()
{
   //some code
   return this;
}

var objf = f(); 

Q2. What does f() returns? global object?

Mahesha999
  • 22,693
  • 29
  • 116
  • 189
  • 1
    You're asking how `this` works in JavaScript? –  May 26 '13 at 17:30
  • 1
    In the first case it depends on the "some code" part, doesn't it? If the function doesn't explicitly return anything its return value is undefined. – JJJ May 26 '13 at 17:31
  • No I have read enough of `this`? :P Just want to know exactly what happens here as asked in questions. Just want to bit more clear and to sum up my understanding of `this`. I have been reading about it for too long. – Mahesha999 May 26 '13 at 17:34
  • You've observed that the first returns `undefined` and the second returns the global object. So it would seem clear that the default return value is `undefined` when there is no `return` statement, and the value of `this` in your second version is the global. –  May 26 '13 at 17:34

3 Answers3

3

Q1. Functions in javascript always return values. The default value is undefined, except for constructors, where the default return value is this. Since no return value is specified in your Q1 example, the interpreter returns the default value, undefined.

Q2. this is a javascript keyword that gets updated by the interpreter depending on application context. In the case of a function that is not called from an object, as in your Q2 example, this will refer to the global object.

How does the "this" keyword work?

Community
  • 1
  • 1
Dan
  • 59,490
  • 13
  • 101
  • 110
  • 2
    I was writing my own take on this, since it was very similar to how Python works with `return` values, in that case if none is defined, it gives `None` so this means they have a similar philosophy in that regard :) +1 – Morgan Wilde May 26 '13 at 17:39
  • 2
    In strict mode, `this` is `undefined` as well – John Dvorak May 26 '13 at 17:50
2

When you do...

function f()
{
   //some code
}

var objf = f();

...you're getting the return value of the "f" function, which in this case is undefined, once there's no "return something" statement.

If you do...

function f()
{
   //some code
}

var objf = new f();

...you'll instance a new object based on the "f" function prototype. Your objf variable will receive a new object reference, even if your "f" function is empty.

And doing this...

function f()
{
   //some code
   return this;
}

var objf = f(); 

..."this", in your case, will be the global object, that's the window object for browsers. The this keywork is a reference to the object to which the function is bound, look at this:

someobj = {
  f:function(){
    return this;
  }
}

var objf = someobj.f();

objf === someobj; //==>true!!!
Alcides Queiroz
  • 9,456
  • 3
  • 28
  • 43
  • and just to add, if we put `return this;` inside function and call it with `new`, it will still return a new object created based on the "f" function prototype - am right? – Mahesha999 May 26 '13 at 17:54
  • The return value of a constructor is used if it is an object; otherwise, `this` is returned from the constructor. – John Dvorak May 26 '13 at 17:57
0

In the first case, objf will indeed be undefined. As the function doesn't return anything, it wouldn't be sensible to just invent something and return that.

In the second case, objf will be the Window object.

Noctua
  • 5,058
  • 1
  • 18
  • 23
  • Ohkay I know `objf` will be undefined, but why? I know when I use `new` to invoke function, it returns `this` if am not explicitly returning anything. Is is just because am not returning anything explicitly and neither am using anything like `new`? – Mahesha999 May 26 '13 at 17:36
  • It's not that the function doesn't return anything. A function that doesn't have an explicit return, has an implicit `return undefined;` – Alan May 26 '13 at 17:59