The constructor example I see a lot is
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = "English";
}
But what if you want many parameters, like 20+. This seems like it would be inefficient. What would be the correct and efficient way of declaring a constructor function that takes in many parameters?
My end goal is to create an image slider with a lot of user settings (slidesToShow, slidesToScroll, slideClass, etc). So I am looking to have the user initialize it like and provide options. The way I have it now is kind of weird lol. example:
function Slider(options) {
const slideClass = options.slideClass
}
and then it is initialized like so:
const demoSlider = new Slider({
slideClass: 'image-slider'
})
But I am not using the this keyword like a normal constructor function and yet it is not a object literal either. So my code must just be built wrong? Just switched from mainly jQuery trying to learn/write vanilla JS the right way.
Thanks for any help in advance!
this.slideClass = options.slideClass?thiswith an options object:this.slideClass = options.slideClass. You can also set a default by using the short circuiting properties of the 'or' operator:this.slideClass = options.slideClass || 'myClass'Object.assign(this, options). But be aware that it is a shallow copy. Or you could just dothis.options = options.