3
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.

5
  • developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/… Commented Apr 19, 2013 at 4: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. Commented Apr 19, 2013 at 4:50
  • Take a look at this: stackoverflow.com/questions/3127429/javascript-this-keyword Commented Apr 19, 2013 at 4:52
  • @AurA—a little weird that MDN categorises this as an operator. It's a keyword. Commented Apr 19, 2013 at 4: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. Commented Apr 19, 2013 at 6:19

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

but why (object.getName)() return "Object"?
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.
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.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.