Last active
August 29, 2015 14:25
-
-
Save ihorkatkov/00249b5e2c72e0f31729 to your computer and use it in GitHub Desktop.
Функция – строковый буфер
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Задача — реализовать строковый буфер на функциях в JavaScript, со следующим синтаксисом: | |
Создание объекта: var buffer = makeBuffer();. | |
Вызов makeBuffer должен возвращать такую функцию buffer, которая при вызове buffer(value) добавляет значение в некоторое | |
внутреннее хранилище, а при вызове без аргументов buffer() — возвращает его. | |
* @example http://plnkr.co/edit/aOwEwSKX2fpwURBVmOqN?p=preview | |
* */ | |
function makeBuffer() { | |
var text = ''; | |
var buffer = function(value) { | |
if (arguments.length == 0) { | |
return text; | |
} | |
text += value; | |
} | |
buffer.clear = function() { | |
text = ''; | |
} | |
return buffer | |
} | |
var buffer = makeBuffer(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment