U
U
Urukhayy2017-05-04 17:03:25
JavaScript
Urukhayy, 2017-05-04 17:03:25

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

2 answer(s)
D
Dmitry Korolev, 2017-05-04
@Apathetic

What you have is a "factory" pattern and also has the right to exist.

C
Cr2ed, 2017-05-04
@Cr2ed

function ClearVehicle(wheelsType, color) { 
    this.wheelsType = wheelsType || 0; 
    this.color = color || 0
}

var vehicle1 = new ClearVehicle(3, 2);
var vehicle2 = new ClearVehicle(4);

if we want to add set methods
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 question

Ask a Question

731 491 924 answers to any question