Answer the question
In order to leave comments, you need to log in
Is it the norm, is there a better option?
Is this use case acceptable, or is it better to make some sort of constructor?
//......
var vehicle1 = newClearVehicle(); // Такой вызов чистого объекта
vehicle1['wheelsType'] = 3;
vehicle1['color'] = 2;
var vehicle2 = newClearVehicle();
vehicle2['wheelsType'] = 4;
//......
function newClearVehicle() {
return {
wheelsType: 0,
color: 0
}
}
Answer the question
In order to leave comments, you need to log in
What you have is a "factory" pattern and also has the right to exist.
function ClearVehicle(wheelsType, color) {
this.wheelsType = wheelsType || 0;
this.color = color || 0
}
var vehicle1 = new ClearVehicle(3, 2);
var vehicle2 = new ClearVehicle(4);
ClearVehicle.prototype.setWheelsType = function(wheelsType) {
this.wheelsType = wheelsTypeж
}
ClearVehicle.prototype.setColor = function(color) {
this.color = color
}
var vehicle = new ClearVehicle();
vehicle.setColor(3);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question