The issue you are facing is caused by the concept of scope and context in JavaScript.
By default when outside of any functions, we are in the global context which means that this refers to the Window object (in a browser, here in a new tab) :
console.log(this); // -> Window about:blank
This is the same when you call it inside a function :
function hello() {
console.log(this); // -> Window about:blank
}
But you can use call() or apply() to pass a context to a function as an object :
function hello() {
console.log("Hello " + this.name);
}
hello.call({name: "Alice"}); // -> Hello Alice
Now in your case, you are creating an anonymous function (a function without a name) that is passed to the changed() method of the checkbox. But when calling this.myvar[x] = 1, this refers to the p5.Element itself, not the current instance of your class.
What you can do is use the Function.prototype.bind() method to bind the current object to the function you pass. It works like this :
let test;
class Test {
constructor() {
this.myvar = [];
this.checkbox = createCheckbox('checkbox');
this.checkbox.changed(function() {
// This is now the current object
console.log(this); // -> Test {myvar: Array[0], checkbox: , constructor: Object}
console.log(this.checkbox.checked()); // -> get checkbox status
this.myvar[0] = 5;
console.log(this.myvar); // -> [5]
}.bind(this));
}
}
function setup() {
test = new Test();
}
The method bind returns a new function with this set to the first argument, in our case the instance of the object. From that we can get the checkbox object.
Thank you Jospehh for the detailed and good explanation! Your solution seems to work, but now I get another error with the checkbox.checked() function:
instead of bind, it’s a bit hacky but you can do something like this too, so that self becomes the “parent” object and still you have access to checkbox’s this:
let test;
class Test {
constructor() {
this.myvar = [];
this.checkbox = createCheckbox('checkbox');
const self = this;
this.checkbox.changed(function() {
console.log(this.checked()); // -> get checkbox status
self.myvar[0] = 5;
});
}
}
function setup() {
test = new Test();
}