简体   繁体   中英

I want to ask the meaning of the following code

Who can tell me the meaning of the following code?

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

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 });

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;

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