SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Closure

  1. #1
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    532
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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()

    Code:
    		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
    "Persistence is the path to perfection"

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by silversurfer5150 View Post
    Is this an acceptable way of performing this task?
    If it works, it's probably acceptable

    Quote Originally Posted by silversurfer5150 View Post
    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?
    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)
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    532
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    "Persistence is the path to perfection"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •