简体   繁体   中英

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;
    }
}

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM