Answer the question
In order to leave comments, you need to log in
Narrowly specialized or broadly?
What is the best way to write functions for a small number of objects?
I will give a vivid example: We have 3 apples of different breeds. The user selects one of the apples and he should be shown a dialog - "Purchasing an apple of breed N ....... Would you like to purchase?", In the dialog there are 2 buttons - "Yes" and "No". Processing the result of the dialog occurs in a separate function.
So. Make a common structure for processing all dialogues, which includes recognition of the apple breed already inside the structure, or for each breed its own structure?
What case 1 would look like (general structure):
function test(){
// Для начала нужно определить породу яблок. В текущей задаче это решаемо только циклом
for(var i in apples)
{
switch(apples[i].type)
{
case 1: // обработка для сорта 1
case 2: // обработка для сорта 2
case 3: // обработка для сорта 3
}
}
}
function test(type){ // порода яблока приходит сразу
// обработка для сорта
}
Answer the question
In order to leave comments, you need to log in
The simplest solution, in my opinion, is to use a list of handlers, something like:
var appleHandler = {};
appleHandler.antonovka = function antonovkaHandler() {
// Обрабатываем
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question