我想问下面代码的含义

时间:2016-07-14 06:32:42

标签: javascript blob zip.js

谁能告诉我以下代码的含义?

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

2 个答案:

答案 0 :(得分:0)

此代码使用以下输入参数创建新的Blob:

  • 由“blob”“array”或“array.buffer”组成的数组。
  • 一个具有一个属性的对象; “type”设置为“contentType”变量。

我猜这是这部分

appendABViewSupported ? array : array.buffer
你在这里想知道。这意味着:如果“appendABViewSupported”为 true ,则使用“array”变量。否则,使用“array.buffer”变量。

此代码段执行相同的操作:

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

但这种方式更优雅:

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

答案 1 :(得分:0)

可以像这样扩展代码(我认为):

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

他们所做的就是压缩一切,使其阅读更加复杂(有人会说)。就个人而言,我会扩展一些并使用一些简短的手。例如,我至少会这样使用三元组:

var a = appendABViewSupported ? array : array.buffer;