【问题标题】:I want to ask the meaning of the following code我想问一下下面代码的意思
【发布时间】:2016-11-16 22:43:35
【问题描述】:

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

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

【问题讨论】:

    标签: javascript blob zip.js


    【解决方案1】:

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

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

    我猜是这个部分

    appendABViewSupported ? array : array.buffer
    

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

    这段代码 sn-p 做同样的事情:

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

    【讨论】:

    • 谢谢你的回答,也许我应该问一下appendABViewSupported的意思。
    【解决方案2】:

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

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 2017-06-03
      • 2020-03-15
      • 2019-11-02
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多