I want to ask the meaning of the following code

57 views Asked by At

Who can tell me the meaning of the following code?

blob = new Blob([ blob, appendABViewSupported ? array : array.buffer ], {
                type : contentType
            });
2

There are 2 answers

1
birgersp On

This code creates a new Blob, with the following input parameters:

  • An array consisting of "blob" and "array" or "array.buffer".
  • An object, which has one property; "type" set to the "contentType" variable.

I'm guessing it's this part

appendABViewSupported ? array : array.buffer

that you are wondering about here. It means: If "appendABViewSupported" is true, then use the "array" variable. Else, use the "array.buffer" variable.

This code snippet does the same thing:

var arrayOrArrayBuffer;
if (appendABViewSupported)
    // If the "appendABViewSupported" variable is true, use the "array" variable
    arrayOrArrayBuffer = array;
else
    // Else, use the "array.buffer" variable
    arrayOrArrayBuffer = array.buffer;

// Create the blob
blob = new Blob([ blob, arrayOrArrayBuffer ], { type : contentType });

But it's more elegant this way:

blob = new Blob([ blob, appendABViewSupported ? array : array.buffer ], { type : contentType });
0
Tigger On

That code can be expanded like this (I think):

var a;
var b;
var c;
var blob;

if (appendABViewSupported) {
    a = array;
} else {
    a = array.buffer;
}

b = [ blob, a ]; // this bit seems like an issue to me but
                 // but would need to see the Blob code.

c = { type : contentType };

blob = new Blob(b,c);

All they have done is compress everything to make it more complex to read (some would say). Personally, I would have expanded some and used some of the short hand. For example, I would have used a ternary at the very least like so:

var a = appendABViewSupported ? array : array.buffer;