Answer the question
In order to leave comments, you need to log in
How to specify the behavior of an object when using the Factory pattern instead of Classes?
Let's say I use the following code to create a newCar object:
const vehicleFactory = function (config) {
const MoveBehaivor = {
move(){
console.log('move')
}
}
const vehicle = Object.assign({},MoveBehaivor)
return vehicle
}
let newCar = vehicleFactory()
newCar.move() // 'move'
const vehicleFactory = function (config) {
const MoveBehaivor = {
move(){
console.log('move')
}
}
const TankMoveBehaivor = {
move(){
console.log('move')
console.log('as tank') // отличие от метода для автомобилей
}
}
switch (config.type) {
case 'car' :
const car = Object.assign({},MoveBehaivor)
return car
case 'tank' :
const vehicle = Object.assign({},TankMoveBehaivor)
return vehicle
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question