When will Object.assign return false

Asked by At

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

2 Answers

2
Murali Nepalli On Best Solutions

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.

1
Jack Bashford On

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