1

I am writing a method that takes two parameters: the first a jQuery object that contains a form, the second an "options" object with default parameters, some of which reference the form in the first parameter.

I know that a default parameter can reference the value of a default parameter which precedes it, so I wonder why my code is failing.

const validation = {
  validateForm(form, {
    btn: form.find('button'),
    action: null,
    displayErrInline: form.attr('validate') === 'inline',
    disableValidation: false
  }) {
    // do stuff
  }
};

Can someone please explain what is wrong with this?

0

1 Answer 1

1

I figured out what I was doing wrong. I was using colons to provide default values to the destructured object instead of the assignment variable.

Here is an example of the correct way to pass default values to a destructured object in a function's parameters:

const validation = {
  validateForm(form, {
    btn = form.find('button'),
    action = null,
    displayErrInline = form.attr('validate') === 'inline',
    disableValidation = false
  }) {
    // do stuff
  }
};

Optionally, I can assign an empty object (= {}) as the default value for the second argument to avoid errors if no second parameter is passed.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.