简体   繁体   English

什么时候Object.assign返回false

[英]When will Object.assign return false

The following code is from the index.js file in the graphql-yoga package. 以下代码来自graphql-yoga软件包中的index.js文件。

Can someone explain to me under what scenario "Object.assign" would return false and trigger the anonymous function being assigned to the variable instead? 有人可以在什么情况下向我解释“ Object.assign”将返回false并触发将匿名函数分配给变量的情况吗?

"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. 在我看来,Object.assign将始终返回true,因为“ assign”是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. ES.5中引入了Object.assign在ES5中不可用。 So I assume the above code is written that way to be compatible with ES5. 因此,我假设上面的代码是以与ES5兼容的方式编写的。

The only case would be if Object.assign didn't exist - in an ES5 environment for instance. 唯一的情况是如果Object.assign不存在-例如在ES5环境中。 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). 在那种情况下,因为您不会尝试调用 undefined ,所以不会导致错误,而是会传递给下一个函数(因为undefined容易出错)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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