So I have this class
definition:
class Field {
constructor(canvas) {
const CANVAS = document.querySelector(canvas);
const CONTEXT = CANVAS.getContext("2d");
return Object.assign(CONTEXT, Field.prototype); // *
}
prototypeMethodName() {
return "something";
}
}
console.log(new Field("canvas"));
<canvas></canvas>
The main idea here is that when I invoke new Field()
I get not the instance of Field
, but the object consisting of two other objects: CONTEXT
and Field.prototype
. The CONTEXT
is the instance of the CanvasRenderingContext2D
, and, basically, I just want to augument it with other methods (in this example it's just one method prototypeMethodName()
).
But in this case I get a bare CONTEXT
object that doesn't contain any of "my" properties.
I also noticed that the assign
method doesn't work correctly only when I'm trying to merge a prototype of something to the tatget object. So the thing here is not about the CONTEXT
object, but is about the prototype.
Why doesn't my code work? What would I do to make it work then?