using an expansion operator, no conversion test



  • <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="utf-8">
      <script src="https://js.cx/test/libs.js"></script>
      <script src="test.js"></script>
    </head>
    
    <body>
      <script>
        "use strict";
    
        function makeBuffer() {
          let text = '';
    
          //используем деструктивные параметры
          return function(...piece) {
            if (piece.length == 0) { // вызов без аргументов
              return text;
            }
            text += piece;
          };
        };
      </script>
    </body>
    
    </html>
    

    Test decision https://plnkr.co/edit/21ZpBDaIF3H2YPPDvbC3?p=preview



  • In this case ... - it's not an expansion operator, it's a syntaxis to determine. https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Functions/Rest_parameters ♪

    That's why. piece inside the function is a mass, and attitudinal mass to line http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.join i.e. if the element null or undefinedit's gonna be an empty line.

    Because it's a test call. buffer(null);inside the function piece=== [null]

    And when it comes to the row, it works. empty line

    In addition, if several parameters are assigned to the buffer function, they will be released through the comma.

    If there is only one parameter to be available, there is no need to use the rest-parameters, it is sufficient to clearly describe one parameter.

    If several parameters are to be transferred, the behaviour remains to be changed. makeBuffer working inside her. piece Like a mass, for example:

    function makeBuffer() {
      let text = '';
    

    //используем деструктивные параметры
    return function(...piece) {
    if (piece.length == 0) { // вызов без аргументов
    return text;
    }
    text += piece.map(el=>el+'').join('');
    };
    };

    let buffer;
    beforeEach(function() {
    buffer = makeBuffer();
    });

    it("возвращает пустую строку по умолчанию", function() {
    assert.strictEqual(buffer(), "");
    });

    it("добавляет аргументы в буффер", function() {
    buffer('Замыкания');
    buffer(' Использовать');
    buffer(' Нужно!');
    assert.equal(buffer(), 'Замыкания Использовать Нужно!');
    });

    it("приводит всё к строке", function() {
    buffer(null);
    buffer(false);
    assert.equal(buffer(), "nullfalse");
    });

    it("добавляет аргументы в буффер, несколько аргументов", function() {
    buffer('Замыкания',' ','Использовать');
    buffer(' ','Нужно!');
    assert.equal(buffer(), 'Замыкания Использовать Нужно!');
    });

    <script src="https://js.cx/test/libs.js"></script>




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2