| Reference | Downloads | Github

How to pass object as argument in function?

URL of experiment:

Description of the problem:

I am trying to pass a variable as an argument in a function that goes into the scheduler. However, it seems that the argument is not being read as a variable, and is undefined instead. Here’s the error message I get:

TypeError: right-hand side of 'in' should be an object, got undefined

The error occurs in the consentRoutineBegin function (I have made a comment below).

Here are bits and pieces of my code that are relevant to this error.

var consentImg1;
function experimentInit() {

	consentImg1 = new visual.ImageStim({
		win: psychoJS.window,
		name: 'consentImg1',
		image: '05550_online_consent_form_pg1.png'

// Routine for consent page 1
var t;
var frameN;
var consent1Components;
function consentRoutineBegin(img) {
	return function (img) {
		//------Prepare to start Routine 'instruct'-------
		psychoJS.window.adjustScreenSize(); // make window fullsize again because the download will exit fullscreen
		t = 0;
		consentClock.reset(); // clock
		frameN = -1;
		// update component parameters for each repeat
		ready.keys = undefined;
		ready.rt = undefined;
		// keep track of which components have finished
		consentComponents = [];

		for (const thisComponent of consentComponents)
			if ('status' in thisComponent) // <- here's the error
				thisComponent.status = PsychoJS.Status.NOT_STARTED;

		return Scheduler.Event.NEXT;

I know that consentImg1 is defined because if I call it directly instead of passing it as an argument (i.e. replace img with consentImg1), it works. It seems that the problem is passing an object as a function.

Thanks in advance!

Is this direct JS code or did you write it in Builder?

If the latter, please could you show your relevant code components?

I’m wondering whether the issue is that your object only exists within experimentinit but it may be that the final code always looks like that.

I took a script that was written in Builder and edited it in JS to suit my needs since what I wanted to do was not easily achievable in Builder.

Here is the code:

It seems like the object only exists within experimentinit, but I am still able to call it directly within the function - I just cannot pass it as an argument (it becomes undefined, not undeclared).

Thanks in advance!

I would recommend that you go back to the Builder version and add code components. I have a crib sheet (see pinned post) on how to deal with the flaws in the auto translate.

What do you feel isn’t achievable via Builder code components?

I’m creating a visual search experiment where the stimuli are in random locations in each trial, with random jitter, and random orientations. There are also some restrictions on which locations are possible depending on the condition. I am able to code it easily in PsychoPy, which is why I tried to translate it to PsychoJS rather than use the Builder.

Either way, using the Builder, I am able to get past my current problem, but I wanted to have a general function that can be re-used by specifying the argument rather than have multiple routines.

I’ve written code components in Builder which create visual objects in a loop with locations randomly taken from an array and then jittered.