Closure

Hi guys I wanted to create a closure to protect data for dynamically swapping between two video players on a page. i.e. Load one then unload and load another. In order to do this correctly I need to know which one is currently on stage.

I have extended the player object allowing me to call this data with: player.get_current_player()


		function Player()
		{
			Player.prototype.set_current_player = function(player){
				this.current_player = player;
			    this.YouTube_initiated = false;
				this.flowplayer_initiated = false;
			};
			
			
			Player.prototype.get_current_player = function() {
			    return this.current_player;
			};
               }

var player = new Player(); ///// Instantiate player object used for flags and changing player id

Is this an acceptable way of performing this task? Am I creating any unnecessary overheads by hiding the data in this way and if so what would be a better way to do it?

thanks

Silversurfer

If it works, it’s probably acceptable :wink:

Unless this code is something that’s going to run very frequently (1,000 … 10,000 times in a row?), performance is probably not an issue here at all, especially considering the only thing you’re doing is setting/getting properties. If this code would be doing DOM manipulation, that would be something to consider optimizing for example.

While I personally would have written it differently, it’s just another way to skin the proverbial cat (and in JavaScript, there are many ways)

HI John,

Thanks for your reply, I am just setting out on my journey into the black art of closures atm. Like everybody says, its a powerful tool but a nightmare to get your head around so your input is appreciated. I figure if I just document each new closure technique I come across and build up a library of different ones then I will know which one to use in a given situation. But saying that I guess every situation is unique…

Anyway thanks again :wink: