When using Javascript as follows:
function Test(Example) {
}
Test.prototype.etc etc etc
function Example() {
}
Example.prototype.etc etc etc
example = new Example();
test = new Test(example);
Is this efficient or am I hogging memory somehow?
I ask this as I have the following setup within my javascript game at present and each requires some of the other (in brackets):
Camera (canvas)
Input (canvas, camera, tilemap)
Sprite (camera, canvas)
Am I going about this the wrong way? Should I be passing this method some other way? It’s struck me as a concern as I now have quite a few new instances of sprite and wondered if it is going to become a problem.
Cheers
JavaScript objects (and functions) are not copied when they are passed as parameters. Take a look at the following code (or test it on JSFiddle):
The
fooobject is not copied when it is passed to thebarfunction. This is demonstrated by the fact thatfoo‘spropertyretains is value back outside of the method. Put simply,fooandparameterpoint to the same object.Keep in mind that they themselves aren’t the same object, though. If you reassign
parameterto point to new value within thebarfunction (parameter = "dummy";),foowill be not be affected. Does that distinction make sense?Note: JavaScript strings and numbers are copied when they are passed as parameters, though.