简体   繁体   中英

When will Object.assign return false

The following code is from the index.js file in the graphql-yoga package.

Can someone explain to me under what scenario "Object.assign" would return false and trigger the anonymous function being assigned to the variable instead?

"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};

It seems to me that Object.assign will always return true as "assign" is a built in method of Object.

var test  = Object.assign;
if(test) console.log("it is true")

the result is "it is true" being printed to the console as expected

Object.assign is not available in ES5, it's introduced in ES6. So I assume the above code is written that way to be compatible with ES5.

The only case would be if Object.assign didn't exist - in an ES5 environment for instance. In that case, because you wouldn't be attempting to call undefined , it wouldn't result in an error and would instead pass to the next function (because undefined is falsy).

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