0

How can I access a property of a require dynamically,

Instead of doing

 let module = require ("path"). get;
 let module = require ("path"). create;

I would like to do;

var property = "get";
let module = require ("path"). property;

var property = "create";
let module = require ("path"). property;

To be able to have a single line and the property dynamically.

1 Answer 1

1

instead of that let module = require ("path"). property;

make that let module = require ("path")[property];

that should work.

If you want a more fancy way

let module = require ("path")[<some_condition> ? 'get' : 'create'];

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

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.