728x90
function AnimalContainter () {
const container = [];
function addAnimal (name) {
container.push(name);
}
function getAllAnimals() {
return container;
}
function removeAnimal(name) {
const index = container.indexOf(name);
if(index < 1) {
throw new Error('Animal not found in container');
}
container.splice(index, 1)
}
return {
add: addAnimal,
get: getAllAnimals,
remove: removeAnimal
}
}
const container = AnimalContainter();
container.add('Hen')
container.add('Goat')
container.add('Sheep')
console.log(container.get()) // ["Hen", "Goat", "Sheep"]
container.remove('Sheep')
console.log(container.get()) // ["Hen", "Goat"]
AnimalContainter에서 반환(리턴)된 변수 / 함수만 외부에서 접근하여 사용가능
728x90
'Web > JavaScript' 카테고리의 다른 글
[Generator] * 과 yield (0) | 2023.11.20 |
---|---|
[Design Pattern] 8.Builder Pattern (0) | 2023.11.18 |
[Design Pattern] 7.Prototype Pattern (0) | 2023.11.17 |
[Design Pattern] 6. Decorator Pattern (0) | 2023.11.16 |
[Design Pattern) 5.Command Pattern (0) | 2023.11.14 |