Class function not working online (ReferenceError: myClass is not defined)

URL of experiment:

Description of the problem:

Class constructor for creating objects with different stimuli attributes does not work online.

I am trying to create objects of my stimuli that consist of different attributes: the path/name, gender, and rating. The error arises when I try to append the object to a list.

Here is my JS code. First I define my class like this:

class stim {
    constructor(img, gender, rating) {
        this.img = img;
        this.gender = gender;
        this.rating = rating;
img = "";
gender = 0;
rating = [];
rated_stimlist = [];

…then I create the attributes:

img = stimlist.pop()
if img.startswith('images/F'):
    gender = 0
if img.startswith('images/M'):
    gender = 1
rating = resp_rating.keys

…then I try to append to a list of objects like this:

rated_stimlist.push(stim(img, gender, rating));

…but for some reason, when I run it online ‘stim’ is not recognized and I get the error:

ReferenceError: stim is not defined

Can someone please help me solve this or suggest an alternative method that gives me an equal list of objects?

I normally use lists of lists, but you could use a list of dictionaries.

Thanks! I’ll try that. But so the class method I used won’t work then?

Actually you might be in luck.

Yes! There was the clue that I needed. I just had to access the class before initializing it like this:

class stim {};
rated_stimlist.push(stim(img, gender, rating));
1 Like

For my own education, please could you explain why / when you would use class instead of dictionaries or lists?

Well, I’m fairly new to programming so I don’t know if it is the best or simplest solution. I just needed to associate a rating value and a gender value to specific images and store all three elements together as objects so that I could compare the objects and find matches on the specific elements to create pairs of stimuli that are unique to each participant and to different parts of the experiment. The class function did the job in python, and I am less familiar with dictionaries.

1 Like