Skip to content

Commit 8a34814

Browse files
authored
Merge pull request #2212 from jspsych/arrow-functions-in-plugins
Consistently replace anonymous functions with arrow functions in plugins
2 parents 309ba0d + ddd5dd1 commit 8a34814

File tree

39 files changed

+132
-173
lines changed

39 files changed

+132
-173
lines changed

.changeset/hip-planets-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@jspsych/plugin-fullscreen": patch
3+
---
4+
5+
Consistently replace anonymous functions with arrow functions to avoid masking of `this`

packages/plugin-animation/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class AnimationPlugin implements JsPsychPlugin<Info> {
103103
this.jsPsych.finishTrial(trial_data);
104104
};
105105

106-
var animate_interval = setInterval(function () {
106+
var animate_interval = setInterval(() => {
107107
var showImage = true;
108108
if (!trial.render_on_canvas) {
109109
display_element.innerHTML = ""; // clear everything
@@ -155,7 +155,7 @@ class AnimationPlugin implements JsPsychPlugin<Info> {
155155
});
156156

157157
if (trial.frame_isi > 0) {
158-
this.jsPsych.pluginAPI.setTimeout(function () {
158+
this.jsPsych.pluginAPI.setTimeout(() => {
159159
display_element.querySelector<HTMLElement>("#jspsych-animation-image").style.visibility =
160160
"hidden";
161161
current_stim = "blank";
@@ -168,7 +168,7 @@ class AnimationPlugin implements JsPsychPlugin<Info> {
168168
}
169169
}
170170

171-
var after_response = function (info) {
171+
var after_response = (info) => {
172172
responses.push({
173173
key_press: info.key,
174174
rt: info.rt,

packages/plugin-audio-button-response/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class AudioButtonResponsePlugin implements JsPsychPlugin<Info> {
106106
// load audio file
107107
this.jsPsych.pluginAPI
108108
.getAudioBuffer(trial.stimulus)
109-
.then(function (buffer) {
109+
.then((buffer) => {
110110
if (context !== null) {
111111
audio = context.createBufferSource();
112112
audio.buffer = buffer;
@@ -117,7 +117,7 @@ class AudioButtonResponsePlugin implements JsPsychPlugin<Info> {
117117
}
118118
setupTrial();
119119
})
120-
.catch(function (err) {
120+
.catch((err) => {
121121
console.error(
122122
`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`
123123
);
@@ -195,7 +195,7 @@ class AudioButtonResponsePlugin implements JsPsychPlugin<Info> {
195195

196196
// end trial if time limit is set
197197
if (trial.trial_duration !== null) {
198-
this.jsPsych.pluginAPI.setTimeout(function () {
198+
this.jsPsych.pluginAPI.setTimeout(() => {
199199
end_trial();
200200
}, trial.trial_duration);
201201
}

packages/plugin-audio-keyboard-response/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {
8383
// load audio file
8484
this.jsPsych.pluginAPI
8585
.getAudioBuffer(trial.stimulus)
86-
.then(function (buffer) {
86+
.then((buffer) => {
8787
if (context !== null) {
8888
audio = context.createBufferSource();
8989
audio.buffer = buffer;
@@ -94,7 +94,7 @@ class AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {
9494
}
9595
setupTrial();
9696
})
97-
.catch(function (err) {
97+
.catch((err) => {
9898
console.error(
9999
`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`
100100
);
@@ -129,7 +129,7 @@ class AudioKeyboardResponsePlugin implements JsPsychPlugin<Info> {
129129

130130
// end trial if time limit is set
131131
if (trial.trial_duration !== null) {
132-
this.jsPsych.pluginAPI.setTimeout(function () {
132+
this.jsPsych.pluginAPI.setTimeout(() => {
133133
end_trial();
134134
}, trial.trial_duration);
135135
}

packages/plugin-audio-slider-response/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class AudioSliderResponsePlugin implements JsPsychPlugin<Info> {
127127
// load audio file
128128
this.jsPsych.pluginAPI
129129
.getAudioBuffer(trial.stimulus)
130-
.then(function (buffer) {
130+
.then((buffer) => {
131131
if (context !== null) {
132132
audio = context.createBufferSource();
133133
audio.buffer = buffer;
@@ -138,7 +138,7 @@ class AudioSliderResponsePlugin implements JsPsychPlugin<Info> {
138138
}
139139
setupTrial();
140140
})
141-
.catch(function (err) {
141+
.catch((err) => {
142142
console.error(
143143
`Failed to load audio file "${trial.stimulus}". Try checking the file path. We recommend using the preload plugin to load audio files.`
144144
);
@@ -252,7 +252,7 @@ class AudioSliderResponsePlugin implements JsPsychPlugin<Info> {
252252

253253
display_element
254254
.querySelector("#jspsych-audio-slider-response-next")
255-
.addEventListener("click", function () {
255+
.addEventListener("click", () => {
256256
// measure response time
257257
var endTime = performance.now();
258258
var rt = Math.round(endTime - startTime);
@@ -285,7 +285,7 @@ class AudioSliderResponsePlugin implements JsPsychPlugin<Info> {
285285

286286
// end trial if trial_duration is set
287287
if (trial.trial_duration !== null) {
288-
this.jsPsych.pluginAPI.setTimeout(function () {
288+
this.jsPsych.pluginAPI.setTimeout(() => {
289289
end_trial();
290290
}, trial.trial_duration);
291291
}

packages/plugin-call-function/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CallFunctionPlugin implements JsPsychPlugin<Info> {
4646
};
4747

4848
if (trial.async) {
49-
var done = function (data) {
49+
var done = (data) => {
5050
return_val = data;
5151
end_trial();
5252
};

packages/plugin-canvas-button-response/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class CanvasButtonResponsePlugin implements JsPsychPlugin<Info> {
145145
for (var i = 0; i < trial.choices.length; i++) {
146146
display_element
147147
.querySelector<HTMLButtonElement>("#jspsych-canvas-button-response-button-" + i)
148-
.addEventListener("click", function (e: MouseEvent) {
148+
.addEventListener("click", (e: MouseEvent) => {
149149
var choice = e.currentTarget as Element;
150150
choice.getAttribute("data-choice"); // don't use dataset for jsdom compatibility
151151
after_response(choice);
@@ -203,7 +203,7 @@ class CanvasButtonResponsePlugin implements JsPsychPlugin<Info> {
203203

204204
// hide image if timing is set
205205
if (trial.stimulus_duration !== null) {
206-
this.jsPsych.pluginAPI.setTimeout(function () {
206+
this.jsPsych.pluginAPI.setTimeout(() => {
207207
display_element.querySelector<HTMLElement>(
208208
"#jspsych-canvas-button-response-stimulus"
209209
).style.visibility = "hidden";
@@ -212,7 +212,7 @@ class CanvasButtonResponsePlugin implements JsPsychPlugin<Info> {
212212

213213
// end trial if time limit is set
214214
if (trial.trial_duration !== null) {
215-
this.jsPsych.pluginAPI.setTimeout(function () {
215+
this.jsPsych.pluginAPI.setTimeout(() => {
216216
end_trial();
217217
}, trial.trial_duration);
218218
}

packages/plugin-canvas-keyboard-response/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {
112112
};
113113

114114
// function to handle responses by the subject
115-
var after_response = function (info) {
115+
var after_response = (info) => {
116116
// after a valid response, the stimulus will have the CSS class 'responded'
117117
// which can be used to provide visual feedback that a response was recorded
118118
display_element.querySelector("#jspsych-canvas-keyboard-response-stimulus").className +=
@@ -141,7 +141,7 @@ class CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {
141141

142142
// hide stimulus if stimulus_duration is set
143143
if (trial.stimulus_duration !== null) {
144-
this.jsPsych.pluginAPI.setTimeout(function () {
144+
this.jsPsych.pluginAPI.setTimeout(() => {
145145
display_element.querySelector<HTMLElement>(
146146
"#jspsych-canvas-keyboard-response-stimulus"
147147
).style.visibility = "hidden";
@@ -150,7 +150,7 @@ class CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {
150150

151151
// end trial if trial_duration is set
152152
if (trial.trial_duration !== null) {
153-
this.jsPsych.pluginAPI.setTimeout(function () {
153+
this.jsPsych.pluginAPI.setTimeout(() => {
154154
end_trial();
155155
}, trial.trial_duration);
156156
}

packages/plugin-canvas-slider-response/src/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class CanvasSliderResponsePlugin implements JsPsychPlugin<Info> {
210210

211211
display_element
212212
.querySelector("#jspsych-canvas-slider-response-next")
213-
.addEventListener("click", function () {
213+
.addEventListener("click", () => {
214214
// measure response time
215215
var endTime = performance.now();
216216
response.rt = Math.round(endTime - startTime);
@@ -228,7 +228,7 @@ class CanvasSliderResponsePlugin implements JsPsychPlugin<Info> {
228228
});
229229

230230
if (trial.stimulus_duration !== null) {
231-
this.jsPsych.pluginAPI.setTimeout(function () {
231+
this.jsPsych.pluginAPI.setTimeout(() => {
232232
display_element.querySelector<HTMLElement>(
233233
"#jspsych-canvas-slider-response-stimulus"
234234
).style.visibility = "hidden";
@@ -237,9 +237,7 @@ class CanvasSliderResponsePlugin implements JsPsychPlugin<Info> {
237237

238238
// end trial if trial_duration is set
239239
if (trial.trial_duration !== null) {
240-
this.jsPsych.pluginAPI.setTimeout(function () {
241-
end_trial();
242-
}, trial.trial_duration);
240+
this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);
243241
}
244242

245243
var startTime = performance.now();

packages/plugin-categorize-html/src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class CategorizeHtmlPlugin implements JsPsychPlugin<Info> {
113113

114114
// hide image after time if the timing parameter is set
115115
if (trial.stimulus_duration !== null) {
116-
this.jsPsych.pluginAPI.setTimeout(function () {
116+
this.jsPsych.pluginAPI.setTimeout(() => {
117117
display_element.querySelector<HTMLElement>(
118118
"#jspsych-categorize-html-stimulus"
119119
).style.visibility = "hidden";
@@ -163,7 +163,7 @@ class CategorizeHtmlPlugin implements JsPsychPlugin<Info> {
163163
});
164164

165165
if (trial.trial_duration !== null) {
166-
this.jsPsych.pluginAPI.setTimeout(function () {
166+
this.jsPsych.pluginAPI.setTimeout(() => {
167167
after_response({
168168
key: null,
169169
rt: null,
@@ -205,7 +205,7 @@ class CategorizeHtmlPlugin implements JsPsychPlugin<Info> {
205205
correct === false &&
206206
((timeout && trial.show_feedback_on_timeout) || !timeout)
207207
) {
208-
var after_forced_response = function (info) {
208+
var after_forced_response = (info) => {
209209
endTrial();
210210
};
211211

@@ -217,9 +217,7 @@ class CategorizeHtmlPlugin implements JsPsychPlugin<Info> {
217217
allow_held_key: false,
218218
});
219219
} else {
220-
this.jsPsych.pluginAPI.setTimeout(function () {
221-
endTrial();
222-
}, trial.feedback_duration);
220+
this.jsPsych.pluginAPI.setTimeout(endTrial, trial.feedback_duration);
223221
}
224222
};
225223
}

0 commit comments

Comments
 (0)