Is there any way that a variable inside a prototype structure can have the same value across all instances? In my case I need to load the Google API, but I want to make sure that the script is only loaded once.
SharedVariable.prototype.createCache = function ( key, requestFunction, callback ) {
// <-- this.cache should be a variable shared across all instances
if ( !this.cache[ key ] ) {
this.cache[ key ] = $.Deferred( function( defer ) {
requestFunction( defer );
}).promise();
}
return this.cache[ key ].done( callback );
}
createCache has three parameters:
keyis basically the src of the scriptrequestFunctionis a function that is only called once perkey.callbackis a function that is called multiple times
This function only works if this.cache is a "global" variable, which should have the same values in all instances. Currently it has a different value for each instance, that's why the code is not working properly. Is something like that possible with prototype?
new SharedVariables). Each instance has a variable called "cache". I want "cache" to have the same value across all instances.