larryq
larryq

Reputation: 16309

What does 'this' represent in the javascript below?

(Sorry, another this question in javascript.)

I have the code below, and I'm wondering what 'this' represents in the call at the end-- the Window or the Bird?

var Bird = (function () {
    Bird.name = 'Bird';

    function Bird(name) {
        this.name = name;
    }

    Bird.prototype.move = function (feet) {
        return alert(this.name + (" flew" + feet + "ft."));
    };

    return Bird;

}).call(this);

Upvotes: 3

Views: 360

Answers (4)

Matt
Matt

Reputation: 41832

Well, assuming there is no parent scope, it is window

EDIT: See example: http://jsfiddle.net/Umseu/1

Upvotes: 7

Igor Besel
Igor Besel

Reputation: 144

Call console.log(this) at first line in the anonymous function. That return the scope, window.

Upvotes: 1

Simon Forsberg
Simon Forsberg

Reputation: 13331

The window. .call(this) is not written inside the bird. It simply calls an anonymous function that happen to return "Bird" "constructor".

Upvotes: 3

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Probably window, because it's not in any particular context that would give this any special meaning.

Upvotes: 5

Related Questions