I need something like so
function updateRender(ClassName){
if(!(this.currentRender instanceof ClassName)){
doPreprocessing();
this.currentRender = new ClassName();
doPostProcessing();
}
}
So I would be able to call updateRender with a new render object, which can be different type.
updateRender( SolidRender );
updateRender( HollowRender );
updateRender( HollowRender ); //does nothing because currentRender is HollowRender
You’ve already got your answer in the comments, so this is just an FYI:
You’re using
this.currentRender, which – if the function is in the global scope – will refer to a variable the global scope. I.e. to thewindowobject of the browser. And putting things in global scope is very rarely a good idea.Technically, you should put all your code in a single namespace or even inside a function that’s invoked immediately so it doesn’t pollute the global scope. However, you can start by simply getting the
currentRendervariable out of the global scope by doing this:the
updateRenderfunction will still be in the global scope, but at least thecurrentRendervariable is safely hidden inside a closure, so onlyupdateRendercan change it (aka privileged access).As for using
klassinstead ofClass, that’s entirely up to you. Usingklassis just a common way of getting around the “classis a keyword” problem in Ruby.