Replies: 3 comments 2 replies
-
Playing more, it seems that the complex (nested) objects are usable, as long as all the fields are public fields (i.e., not prefixed with a No methods on objects seem to be available, however. |
Beta Was this translation helpful? Give feedback.
-
Hey @awebb-nz, I suspect what might be happening is that function is being treated as a dynamic parameter. It is therefore evaluated at the onset of the trial. To block this behavior, you can note the expected type of the nested parameter as a function, and then jsPsych should leave it alone. Here's an example of a nested parameter type in the survey-text plugin: jsPsych/packages/plugin-survey-text/src/index.ts Lines 6 to 49 in 8099b3f And here's an example of using a function type on a parameter: jsPsych/packages/plugin-call-function/src/index.ts Lines 5 to 11 in 8099b3f |
Beta Was this translation helpful? Give feedback.
-
Hi @jodeleeuw, thanks for your reply! I think we are talking about slightly different things, so I will elaborate a bit. As I mentioned in my first message, what I am describing as surprising behaviour might just be the intended behaviour, and I may have to realign my thinking. In any case, here goes: I have a class which defines a class Cell {
...
get current() {
return ...
}
}
class Grid {
constructor(state) {
this.cells = new Map();
... //fill in the map using the initial state
}
current(row, col) {
return this.cells[[row, col]].current
}
} The idea is to have the slightly complex behaviour encapsulated. I pass a var grid = new Grid(state);
var mcg = {
type: jsMulticlickGrid,
clicks: 5,
grid_state: grid
};
timeline.push(mcg); If I run the following code outside of the plugin it works, and it gives me the types and values I expect: console.log(grid);
console.log(grid.current(1, 1)); So, it knows that console.log(trial.grid_state);
console.log(trial.grid_state.current(1, 1)); When I look at the logged object I can see that, although it can still access the direct fields of the object, it has lost all type information, so that As mentioned, I can work around this, but I am just wondering if that is the expected behaviour. I noticed while playing with it, that I can work around in a sort of OK way, by changing the methods to be defined as function fields on the object, rather than methods on the prototype, as follows: class Grid {
...
current = (row, col) => {
return this.cells[[row, col]].current()
}
} and then it works as I expected it to. Thanks for your help! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there, I am just getting started writing new plugins for a jspsych-based experiment. I have run into some behaviour that I find a bit strange, and I just want to check whether it is the intended behaviour or not. Specifically, I want to pass an object parameter, so I have the following in my plugin javascript file:
and I pass an instance of the object in:
However, when I try to call a method on the
trial.grid_state
object in my plugin code, I get an Uncaught TypeError, which tells me the function doesn't exist (if I call the function from outside the plugin, it works fine).So, this suggests to me that the object that given as a parameter is not actually passed in to the plugin, but instead a copy of the public fields of the object are passed in. Is that the intended behaviour, and if so is there any way to pass in a more complex object?
I realise I can decompose the object into individual fields, and move the functionality into the plugin code itself, and I will do that if I have to. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions