0

Let's suppose actions is defined but how do I access this first and second boolean inside someFunction?

is this an object or some kind of destructuring? cause I am getting a some kind of destructuring error.

function someFunction(actions, { first = true, second= false }) {      
    if(first == true) {
      console.log("something");
    }
}   
  

Error i get -

TypeError: Cannot read property 'first ' of undefined
3
  • 3
    If you're getting an error then you should always include it in your question. Commented Sep 9, 2020 at 15:44
  • @TimLewis this was the code i given and I have to do it that way. Commented Sep 9, 2020 at 15:46
  • 1
    That's fine, but if the code you were given is not working, you can't really do it that way can you :) Anyway, you've been asked to include the error you're getting, so please update you question with that information. Commented Sep 9, 2020 at 15:47

1 Answer 1

1

For that to work you have to pass a second parameter when you call the function.
The second argument will be destructured; delayedActions("foo", "bar") will work, but delayedActions("foo", {}) makes more sense.

You can override the default value of recursive:
delayedActions("foo", { recursive:false })

The object parameter can include other things not in the function signature object:
delayedActions("foo", { recursive:true, errorExit:true, fooParam:'bar' })

Without a second parameter you get an error such as
VM48976:1 Uncaught TypeError: Cannot read property 'recursive' of undefined

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, I get exactly that error. thank you so much. Is that just an object? I thought it was default parameter.
@AlmuntasirAbir well it is a default parameter but it is an unnamed default parameter. A default parameter would usually have a name... function delayedActions(actions, config = { recursive = true, errorExit = false }) but that doesn't work with an object param, saying "Invalid shorthand property initializer" — but that may be something I don't understand.

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.