joshschreuder
joshschreuder

Reputation: 1463

Can 'this' be undefined from within a prototype function?

Is it possible for this prototype function to return false or is it a pointless check that will always return true?

if (!Date.prototype.hasOwnProperty("foo")) {
    Date.prototype.foo = function () {
        var date = this;
        if (!date) {
            return false;
        }
        return true;
    }
}

Upvotes: 2

Views: 76

Answers (1)

caleb531
caleb531

Reputation: 4361

The this keyword in JavaScript is only undefined if strict mode is enabled. Otherwise, it will reference the window object by default, unless any of the following are true:

  • The function is called using the new operator
  • A different object is bound to this using call() or apply()

Upvotes: 4

Related Questions