0

I have found that this syntax is valid for defining a function

    let path = finalObjs.vendorPath
    const getAllIndexes = (dependenciesResultToHydrateFlat, path) => {

    } 

However, this syntax is invalid

    const getAllIndexes = (dependenciesResultToHydrateFlat, finalObjs.vendorPath) => {

    } 

Why is it not allowed to use dot notation directly to reference a property value when defining a function? It feels like these two are functionally the same and I don't understand why the latter is invalid. Explanation from the wizards is appreciated.

7
  • 1
    Those are parameter names, not expressions. That doesn't actually make any sense. Commented May 13, 2019 at 20:30
  • 1
    And let path in your first example doesn't actually do anything. Commented May 13, 2019 at 20:30
  • Path is equal to the vendorPath field on the finalObjs object Commented May 13, 2019 at 20:31
  • 1
    Are you aware that the two instances of path in your first example are completely different entities that just happen to have the same name? Or is that part of your question? Commented May 13, 2019 at 20:31
  • Fwoop, duh. makes sense Commented May 13, 2019 at 20:32

2 Answers 2

1

(I know this might neglect but it could be helpful)

You also can set default values for your function parameters.

Code:

const getAllIndexes = (dependenciesResultToHydrateFlat, path=finalObjs.vendorPath) => {

} 

With this, you can change the path afterwards by giving it a value. Otherwise it would use the 'default' value you set to the specific parameter.

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

2 Comments

This is awesome! Thank you kindly
@Goldfish Thank you! Happy to help!
0

Parameters can be named anything, a given parameter is the name that will be assumed for that argument at the time of the function call

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.