1

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!

9
  • 2
    Having 20+ parameters is a sign of bad design. If you really need to do that, you should make them named parameters (as you're doing now). Commented Apr 16, 2018 at 14:05
  • 1
    What's stopping you from doing this.slideClass = options.slideClass? Commented Apr 16, 2018 at 14:07
  • 1
    You can (should?) still use this with 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' Commented Apr 16, 2018 at 14:07
  • I was going to edit because I re-read it and was like hmm maybe THAT is the issue lol. But I'm not sure any other way to allow the user to set multiple options... Commented Apr 16, 2018 at 14:07
  • 1
    You could also copy all the properties at once like Object.assign(this, options). But be aware that it is a shallow copy. Or you could just do this.options = options. Commented Apr 16, 2018 at 14:07

2 Answers 2

3

Either just store an object under this.options :

 function Slider(options) {
    this.options = options;
 }

Or if you want it directly under this, just use Object assign:

 function Slider(options) {
   Object.assign(this, options);
    console.log(this.name);
 }

 new Slider({ name: "test" });
Sign up to request clarification or add additional context in comments.

1 Comment

So really what I am doing now is fine? I guess the only difference is that I have put the options into variable names. Is that even necessary or should I just be using it like "options.slidesToShow" in my code instead of putting options.slidesToShow into a variable?
0

I think you better set them one by one, because they might have default value, ex:

this.height = (options.height)?options.height:100;

Also I think you're not quite understand the meaning of new.

You didn't use this which means all variables are in your function,

maybe in this case you only need to execute that function once to init your slider's event.

so you only need to call Slider(options) .

new Slider() will create a Object that can be reuse,

but you didn't even assign to a var like var mySlider = new Slider()...

You should consider in this case whether you need to design your function as a function or a constructor?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.