In the browser there is not a gl object until you make one:
const gl = canvas.getContext('webgl2')
This means that there it is challenging to reference any of the gl properties (like gl.createBuffer or gl.ARRAY_BUFFER) in something like a class.
In C++ these objects are included and therefore accessible without a a particular instance of gl.
How can I create a class which has a member variable whose value is set to some property of a gl object?
For instance:
class App {
constructor () {
this.VBO = gl.createBuffer() // gl is not defined here
}
}
I think the most common way is to pass in a gl instance into the constructor (i.e.):
class App {
constructor (gl) {
this.VBO = gl.createBuffer() // gl is passed in
}
}
but this seems like it would get annoying if I have to make hundreds of different calls and pass in a gl object each time.
Additionally I guess I could define some sort of global gl object:
gl = canvas.getContext('webgl2') and then just assume that there is some global gl object everywhere it needs to be accessed, but this seems like a very bad idea.
I would appreciate any ideas on a good, clean design paradigm to get around these issues.
prototype. Then you can access it likethis.g1glbefore instantiating those class objects.extendskeyword otherwise. See my answer below