enjoyjeremy enjoyjeremy - 3 months ago 52
Javascript Question

Bigcommerce Stencil: Javascript Error "Object doesn't support property or method 'assign'" in Windows Safari and IE

Not sure what's happening but this error is preventing the page to display.

The error message is pointing to my bundle.js file (stencil bundle with webpack, babel, etc) and specifically to the

Object.assign()
method in the stencil-utils package. It's line 27 of
@bigcommerce/stencil-utils/src/lib/request.js


Here's the section of code producing the error.

const defaultOptions = {
method: 'GET',
remote: false,
requestOptions: {
formData: null,
params: {},
config: {},
template: [],
},
};
const options = Object.assign({}, defaultOptions, opts);
const data = options.requestOptions.formData ? options.requestOptions.formData : options.requestOptions.params;
const headers = {
'stencil-config': options.requestOptions.config ? JSON.stringify(options.requestOptions.config) : '{}',
'stencil-options': '{}',
};


Any ideas of what might be causing this?

Answer

Object.assign is quite new, implemented in ES6; only newer browsers will certainly support it.

Anyway, there's a polyfill of Object.assign in this page. You can apply it in the top of your code.

I written my own polyfill, which does completely copy objects properties (including arrays) to a specific object. This is more safe:

if(typeof Object.assign !== "function")

    Object.assign = function() {

        var target;

        var locations = [],
            last;

        function consume(obj) {

            if(!last) last = target;

            for(var key in obj) {
                if(typeof obj[key] === "object") {
                    last = last[key] = obj[key] instanceof Array ? [] : {};
                    locations.push(last);
                    consume(obj[key]);
                } else last[key] = obj[key];
            }

            var len = locations.length;
            // locations.length --; isn't cross-browser
            locations.splice(--len, 1);
            last = locations[--len] || target;

        }

        for(var i = 0, copy; copy = arguments[i]; i ++) {

            if(typeof copy === "object") {

                if(typeof target === "object") {
                    consume(copy);
                }else target = copy;

            }

        }

        return target;

    };
Comments