Best practice to modularize an experiment #2442
-
Hey y'all! I'm currently refactoring some code and decided to make the switch to npm. The main motivation was to be able to sperate my source code into several files that should be easier to track. For example, I was thinking of having a So far, I have been able to do what I wanted to do and I can see how it would make maintaining the code easier. However, I'm facing a problem which make me doubt of my approach. In some of the I have been able to fix this problem by defining jsPsych with As a newbie (had to learn js by myself), there's probably a lot of things that I am not considering, and I'd be very happy to get your thoughts on this! Please let me know if I can be clearer or can provide additional informations! Cheers, and thanks to the dev team for the amazing work, |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
I'm going to tag @bjoluc in on this since I expect he will know the best approach. I think my approach, FWIW, would be to export a function from each of the |
Beta Was this translation helpful? Give feedback.
-
@jodeleeuw's factory function suggestion is right on the money! In case you are using jspsych-builder (or configured your bundler to use Babel), you may also find https://github.com/mbeaudru/modern-js-cheatsheet helpful 🙂 |
Beta Was this translation helpful? Give feedback.
-
It makes total sense! Thank you for the swift response, it is greatly appreciated! And the cheatsheet will also be super helpful! Thanks! It is a journey to update my ~2005-high school's understanding of javascript. 😉 |
Beta Was this translation helpful? Give feedback.
-
Hi there, Here, i'd edit
And then, I would pass my jsPsych experiment in this function in
At least, this is what I ended up implementing. Works very well, especially regarding code recycling for new experiments. |
Beta Was this translation helpful? Give feedback.
I'm going to tag @bjoluc in on this since I expect he will know the best approach.
I think my approach, FWIW, would be to export a function from each of the
timeline-xxx.js
files that generates the correct timeline and takes in an instance ofjsPsych
as an argument. Then you can create thejsPsych
object in scope for themain.js
file without attaching towindow
. But I wouldn't be surprised if there is a better way to do it that I'm not thinking of.