4
var name = "the Window.";
var object = {
    name:"Object",
    getName: function(){
        return this.name;
    }
}
(object.getName)(); //"Object"
(object.getName = object.getName)(); //"the Window"

I run this code, and it return "the Window", while i think it should be "Object".Please tell me why? thanks.

Mr.Huang
  • 55
  • 4
  • https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/this – AurA Apr 19 '13 at 04:49
  • The value of *this* inside a function depends on *how it is called*, not *how or where it is defined*. `this` must definitely be the most talked about thing in SO. – techfoobar Apr 19 '13 at 04:50
  • Take a look at this: http://stackoverflow.com/questions/3127429/javascript-this-keyword – Ezhil V Apr 19 '13 at 04:52
  • @AurA—a little weird that MDN categorises *this* as an operator. It's a keyword. – RobG Apr 19 '13 at 04:57
  • Please also consider strict mode where when a function is not called with a specific context or by the owner object, `this` is undefined and not a fallback to the global context. – atondelier Apr 19 '13 at 06:19

4 Answers4

3
var name = "the Window.";

Global declarations create a property of the global/window object. This is equivalent (more or less) to:

var global = this;
global.name = 'the Window';

The expression:

(object.getName = object.getName)

returns the function referenced by object.getName. The following empty parameter list (i.e. the ()) causes it to be called.

Since the this value is not set by the call, it defaults to the global/window object, so the function returns the value of global.name.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • but why (object.getName)() return "Object"? – Mr.Huang Apr 19 '13 at 05:09
  • Because when you call the function as a method of `object`, its `this` references `object`. So `this.name` returns `object.name`, which is the string "Object". Perhaps you should use some different values to make it clearer. – RobG Apr 19 '13 at 05:35
1

The bottom line of confusion is we are trying to execute

(object.getName = object.getName)();

and we think it should print "Object".

Actually, it won't. Here is a simple reason. If you break this statement into 2 statements you will get it.

  • Assignment: object.getName is assigned some handler. In this case, it is assigned to itself.
  • Execution of handler. Now, the handler is exected but we do not have context this time. Handler is executed by window. So you are getting window.name which is 'the window.'

Try this

var x = object.getName;
x();

It is somewhat similar to your case and it gives 'the window.' too for the same reason x is executed by window.

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
0

Instead of thinking of this in the classic OOP way you're used to, think of this as the context on which a function is called.

In your sample code, window is the context for the getName call, regardless of where that function is declared.

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
0
var x = object.getName;
x();

is not quite the same. the "=" only make sure that x refer to the same memory address as object.getName, but x belongs to window while object.getName belongs to object. I think the problem comes from the return value of the assignment expression. maybe there is a temporary variable to receive the value of (object.getName = object.getName), that is,

var temp = (object.getName = object.getName);
temp();//"the Window"

thus may make sense.

Boki
  • 31
  • 1