简体   繁体   中英

What is the best way to construct this object?

I have an object, say "car":

function car(name, speed, options){
  this.name = name;
  this.speed = speed;
  this.options = options;
}

There will be multiple "options", so I was thinking of passing it an array:

var carMustang = new car("Mustang", 250, ["Cruise Control","Air Conditioning", "ABS"]);

This doesn't work, as passing an array to a function needs to be done differently, as I have been reading.

But my real question is: Is this an effective way to build these objects? If you had to pass multiple properties to an object, and there will be MANY objects, how would you go about it?

Bonus: if you could solve my issue with passing array information to an object constructor, I'd be forever grateful.

Thanks

class Car {
    constructor(_name, _speed, _options){
        this.name = _name;
        this.speed = _speed;
        this.options = _options;
    }

    addToOptions (newOption) {
        this.options.push(newOption);
    }
}

var car = new Car('mustang', 250, [1, 2, '3']);

console.log(car);

car.addToOptions('AC');

console.log(car);

If you have a lot of options you might consider creating some sort of object to store them in and then pass the object as the parameter. The object might just have a property for each possible option and then just set them to true or false for whether this car has it or not. As far as the passing of an array goes consider that when an array is passed through a function it decays to a pointer to that array. This means that if you pass it as a parameter and then later on change the values of that array outside of the function it will also change the values within the function.

In es6 you can simulate named parameters. And default them if you like. When its 3 arguments it really does not matter. My point is when its many arguments. The order you pass them does not matter with this approach. If you want to pass optional arguments its much easier when the order of the arguments does not matter. I think this is a clean way to to it. At least the es6 way.

function Car({ name, speed = 50, options }){
  this.name = name;
  this.speed = speed;
  this.options = options;
}

const car = new Car({ speed: 100, name: 'Volvo' });

//Car { name: 'Volvo', speed: 100, options: undefined }
console.log(car)

Pre es6

function Car(config){
  this.name = config.name;
  this.speed = config.speed || 50;
  this.options = config.options;
}

const car = new Car({ speed: 100, name: 'Volvo' });

//Car { name: 'Volvo', speed: 100, options: undefined }
console.log(car)

Say you want d and e to be optional in the function below. Then you need to put them last or pass null/undefined/whatever_you_prefer

function Car(a, b, c, d, e) {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM