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

  • Home
  • SEARCH
  • 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 5942727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:17:39+00:00 2026-05-22T16:17:39+00:00

I want to create a javascript library, so I thought making it an immediately

  • 0

I want to create a javascript library, so I thought making it an immediately self executing function would be a nice thing to do to ensure scope safety and everything.

But now I’m running into a problem with using the “this” keyword that I don’t quite understand.

How can I make a code like this work correctly? Currently, it tells me that “image” is undefined.

(function() {

    function lib() {
        this.image = document.getElementById("image");
        this.parts = [
            {name: "part1", num: "1"}
        ];

        this.init = function() {
            $(parts).each(function() {
                var partNum = this.num;
                image.getElementById(partNum).addEventListener("click", function() {
                    toggleHighlight(partNum);
                }, true);
            });
        };
    }

    window.lib = new lib();
})();

window.lib.init();

How can I access the image property?

  • 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-05-22T16:17:40+00:00Added an answer on May 22, 2026 at 4:17 pm

    I think you have a few issues here:

    • you are referencing parts in your init function without accessing it as a property of this.
    • within the each block, you are accessing image as a global variable but it is not declared globally. You can’t access image from this in that block because in that context, this is actually set to the item you are iterating on. (That is why you can access this.num.)

    I sugggest you include var that = this; before your init function and use it when accessing properties like that.image.

    var that = this;
    this.init = function() {
        $(this.parts).each(function() {
            var partNum = this.num;
            that.image.getElementById(partNum).addEventListener("click", function() {
                toggleHighlight(partNum);
            }, true);
        });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure how I would do this in Javascript. I want to create
I'm using the Raphaël—JavaScript Library to create some pie chart graphics, but I want
Im trying to find out a good JavaScript library that can create a nice
I want to create a barchart using the gRaphael javascript library. Does gRaphael support
I want to create a compiled JavaScript file for my website. For development I
I want to create a string in JavaScript that contains all ascii characters. How
I want to create a draggable and resizable window in JavaScript for cross browser
Basically I want to create one large object of many object in JavaScript. Something
I create a file in JavaScript. Now I want to be able to make
Im using the Prototype Javascript Library's Window class to create pop up windows in

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.