S
S
SM_ST2022-02-08 17:01:46
typescript
SM_ST, 2022-02-08 17:01:46

Initialize parameters in JavaScript?

Hello, I'm trying to write my own carousel

, there is a file with default.ts settings

export default {
  container: '.carousel',
  slidesToShow: 4,
  slidesToScroll: 1,
}


and the carousel class itself

import params from './carousel/default'

export class Carousel {
  constructor(
    ...args
  ) {
    if (args.length > 0) {
      if (typeof args[0] === 'string') {
        params.container = args[0]
      }
    }
    let vpar = args[1]
    for(var item in vpar) {
      console.log(vpar[item])
    }
  }
}


I initialize the carousel like this

new Carousel('.carousel', {
        slidesToShow: 4,
        slidesToScroll: 1,
})


There can be many parameters when calling, how can I assign them to values ​​from the default.ts file in the constructor if there are such parameters

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2022-02-08
@SM_ST

const args = {slidesToShow: 2}

const defaultValues = {
  container: '.carousel',
  slidesToShow: 4,
  slidesToScroll: 1,
}

const resolved = Object.assign({}, defaultValues, args)
//  { container: ".carousel", slidesToShow: 2, slidesToScroll: 1 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question