Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8095267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:04:09+00:00 2026-06-05T21:04:09+00:00

I am using Lightbox2 https://github.com/lokesh/lightbox2/blob/master/js/lightbox.js And I don’t understand why all the inner members

  • 0

I am using Lightbox2

https://github.com/lokesh/lightbox2/blob/master/js/lightbox.js

And I don’t understand why all the inner members of Lightbox are prototyped (Lightbox.prototype.init) and not simply members (Lightbox.init)?

If they are specific to each instance of lightbox wouldn’t it be easier to use this.init?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:04:11+00:00Added an answer on June 5, 2026 at 9:04 pm

    Confused? Don’t be…

    Think of it this way:

    1. Lightbox is your class definition, but it’s not yet an instance.

    2. Whatever you put directly on the class is like a static member:

      Lightbox.staticFunc = function() {
          // "this" will not point to instance object
      };
      
    3. Whatever you put on its prototype is a shared instance member:

      Lightbox.prototype.instanceFunc = function() {
          // "this" will point to object instance so members can be accessed
      };
      
    4. When you create an instance of a class, all instance members are accessible throught this keyword, but static ones through class definition:

      var someData = Lightbox.staticFunc();
      var l = new Lightbox();
      l.instanceFunc();
      

    Does this clear you understanding of prototype members?

    Lightbox code then

    The code that you’ve been looking at means this:

    // this is a constructor that accesses instance properties (using "this")
    // ------
    // since properties are accessed via "this.something" means that they are
    //    not shared between instances but are part of one particular instance
    // ------
    function Lightbox(options) {
        this.options = options;
        this.album = [];
        this.currentImageIndex = void 0;
        this.init();
    }
    
    // adding an instance method that will be accessible to lightbox object instance
    //    that's why it can also access instance members (using "this")
    // ------
    // all functions that are defined on the prototype are shared between
    //    all instances so they consume less resources because not every
    //    object instance created them separately.
    // ------
    Lightbox.prototype.init = function() {
        this.enable();
        return this.build();
    };
    

    But some parts of this code are a bit confusing i.e.

    LightboxOptions = (function() {
    
        function LightboxOptions() {
            this.fileLoadingImage = 'images/loading.gif';
            this.fileCloseImage = 'images/close.png';
            this.resizeDuration = 700;
            this.fadeDuration = 500;
            this.labelImage = "Image";
            this.labelOf = "of";
        }
    
        return LightboxOptions;
    
    })();
    

    LightboxOptions class is contained within a function closure even though it doesn’t define any private data, so the outer immediately executing function could be omitted in this example while having identical results:

    LightboxOptions = function() {
        this.fileLoadingImage = 'images/loading.gif';
        this.fileCloseImage = 'images/close.png';
        this.resizeDuration = 700;
        this.fadeDuration = 500;
        this.labelImage = "Image";
        this.labelOf = "of";
    };
    

    It would of course be possible to define those functions in a constructor using this but then they wouldn’t be shared between instances hence every object instance would define the same function hence consuming more resources. So this is not the same although from the execution point it does look the same:

    CustomClass = function() {
        this.prop = true;
    };
    CustomClass.prototype.method = function() { alert("I'm shared."); };
    

    is slightly different than:

    CustomClass = function() {
        this.prop = true;
        this.method = function() { alert("I'm duplicated in every instance."); };
    };
    

    The later consumes more resources while function is defined for every object instance.

    …and a bit more to completely clear this thing

    Suppose we have this class definition:

    var C = function() {
        this.prop = true;
        this.method = function() { console.log("Per instance method"); };
    }
    C.prototype.method = function() { console.log("Shared instance method"); };
    

    What happens here if we call these lines of code

    var a = new C();
    var b = new C();
    a.method();
    b.method();
    delete a.method;
    a.method();
    b.method();
    

    What do you think the output would be? You should get at least a little confused what happens after delete? Which method will get deleted? Per instance? Shared? Both? Well as it should be per instance method gets deleted on object instance a, that’s why afterwards it reports that the shared method has been called. But only on a. b still has its own per instance method.

    So without any further ado, output looks like this:

    Per instance method      // a.method
    Per instance method      // b.method
    Shared instance method   // a.method
    Per instance method      // b.method
    

    What about prototype properties

    These are different. When you create an object instance all those properties get copied to every object and are not shared. So whatever you do on them within the realm of a particular object will not get reflected to others.

    If you’d then delete such property on a particular object it would still be available with its initial value as it was when object got instantiated.

    var C = new function() {};
    C.prototype.prop = 1;
    
    var a = new C();
    var b = new C();
    
    a.prop = 10;   // does not change the value of "b.prop"
    delete a.prop; // "a.prop" is now back to 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some jQuery assistance. I'm using https://github.com/jbutz/bootstrap-lightbox for my lightbox, basically what I
I'm using @Premasagar's nitelite jquery plugin https://github.com/premasagar/nitelite to create a lightbox for my site.
I am currently using Lightbox 2 (http://lokeshdhakar.com/projects/lightbox2/) and this is all working fine, however
I am using a jquery lightbox plugin ( http://colorpowered.com/colorbox/ ) to load content using
I am using jquery lightbox plugin colorbox ( http://colorpowered.com/colorbox/ ) and i want to
I was wondering if using Lightbox all over the site would be a smart
I have an URL like this: http://site.com/galleries/galleryname/imageID123/lightbox/ and using a script to replace the
I am opening a an ajax window using prettyPhoto: http://www.no-margin-for-errors.com/projects/prettyphoto-jquery-lightbox-clone/ This is my link
I am currently using light box provided by ... http://leandrovieira.com/projects/jquery/lightbox/ How do I have
I am using the following lightbox: http://serennz.sakura.ne.jp/toybox/lightbox/ It's simple and light. However, I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.