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 6869953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:37:21+00:00 2026-05-27T03:37:21+00:00

I have a little library that can perform methods on a specific DOM element.

  • 0

I have a little library that can perform methods on a specific DOM element. It looks a little like this:

//An accessor method for the library
window.$ = function(selector){
    var new_instance = new lib();
    return new_instance.init(selector);
}
//And the actual library...
function lib(){
    this.context = window;
    this.init = function(sel){
        this.context = document.querySelector(sel);
        return this;
    };
    this.addPlugin = function(name, fn){
        lib.prototype[name] = fn;
        return this;
    };
}

Like Prototype or jQuery, you select the element with a $(_selector_) call. To add a method to the library, you would use the lib.addPlugin(...) method. Unfortunately, when I add a method like so:

lib.addPlugin('foo',function(){
    console.log(this.context);
    return this;
});

The THIS keyword refers to window, not the original parent library instance. I don’t know much about how all the prototype stuff works, but I am wondering if there is someway I can get around/fix this. I would like newly added methods to be able to reference the parent library’s variables.

  • 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-27T03:37:22+00:00Added an answer on May 27, 2026 at 3:37 am

    Do not redefine functions in the constructor, you are creating a closure every time you call $, which uses a lot of memory.

    Anyways I didn’t see any other problem other than addPlugin being an instance method instead of static method which I implemented like this:

    function lib(){
    this.context = window;
    }
    
    lib.addPlugin = function(name, fn){
      this.prototype[name] = fn;
      return this;
     };
    
    lib.prototype.init = function(sel){
    this.context = document.querySelector(sel);
    return this;
    };
    

    jsfiddle: http://jsfiddle.net/rAasm/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# class library that contains methods that need to be used
We have a node.js app that uses node_msyql , a great little library for
i have little problem with boost::asio library. My app receive and process data asynchronously,
I have little question about how web browser retrieve webpage? I know this User
I have little doubt about string reading in C. string reading functions like gets,
I have a MainMenu in my app and I would like to have little
I once found a nifty little library that used JNI to allow java applications
We have a C++ library that we provide to several different clients. Recently we
I have a WPF app that from time to time needs to perform a
I'm writing a little library where you can set a range; start and end

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.