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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:32:54+00:00 2026-06-10T04:32:54+00:00

In attempting to keep with the DRY principle, I decided to revise a function’s

  • 0

In attempting to keep with the DRY principle, I decided to revise a function’s prototype in an effort to reduce the number of function.call() calls.

Following is a snippet of what I currently have and further clarification as to what I am attempting to do.

com.domain.$ = function(s){
     if(!this){return new com.domain.$(s);}

    this.selector = s;
    this.elements = document.querySelectorAll(s);
}

com.domain.$.prototype = (function(){
    function exe(f){
        var e = this.elements,
            el = e.length;
        for(var i=0; i<el; i++){
            f(e[i]);
        }
    }

    function addClass(c){exe.call(this,function(el){el.classList.add(c);});}
    function removeClass(c){exe.call(this,function(el){el.classList.remove(c);});}
    function toggleClass(c){exe.call(this,function(el){el.classList.toggle(c);});}

    return {
        addClass:addClass,
        removeClass:removeClass,
        toggleClass:toggleClass
    }
}());

I realize this looks very much like I am attempting to mimic the functionality of jQuery. While intentional, this is not meant to act as a replacement but rather a better personal understanding of JavaScript.

That said, what I would like to do is remove the need to invoke exe() via exe.call(this[, fnc]); in order for the context of this to be what I want. I believe I can do this through function binding (.bind()), though perhaps not the way I would like to. I understand it is possible to instead do something like:

com.domain.$.prototype.exe = function(){}

and call it like:

function addClass(c){this.exe(function(){});}

In doing so, however, I lose the private visibility of exe() provided by the closure in my original code. I would like to keep that in tact, if possible.

My question, then, is whether or not it is possible to bind exe() within my original code in such a way that I can reduce the redundant use of exe.call(this, possess the correct context for this within exe(), and maintain the private visibility within the closure?

If this seems a poor implementation of what I am trying to accomplish, I am more than happy to consider other options.

Thank you in advance.

  • 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-10T04:32:56+00:00Added an answer on June 10, 2026 at 4:32 am

    No, you can’t, since at the moment you define exe(), the instance you want to call exe() on does not exist yet.

    In fact, if you are going to make several calls to com.domain.$, you will use exe() with different instances and therefore it does not make sense to bind exe() to a specific instance.

    If you wanted to do that, you’d have to define all these methods inside the constructor and you would loose all the advantages of prototypes:

    (function() {
        function exe(f){
            // ...
        }
    
        com.domain.$ = function(s){
            // ...
    
            var exe_ = exe.bind(this);
            this.addClass = function(c) {
                 exe_(function(el){el.classList.add(c);});
            };
            // ...
        };
    }());
    

    I would suggest, if you don’t want to use .call, to just modify exe() so that it accepts an array of elements as argument and pass this.elements to it from the prototype functions. I don’t see why exe() needs to use this at all. It’s merely a helper which passes each element of an array to a given function and by making it more general, it is easier to reuse.
    For example:

    var com.domain.$ = (function(o) {
        function exe(arr, f){
            var el = e.length;
            for(var i=0; i<el; i++){
                f(arr[i]);
            }
        }
    
        var $ = function(s){
             // ...
        }
    
        $.prototype.addClass = function(c){
            exe(this.elements, function(el){
                el.classList.add(c);
            });
        };
        // ... 
    
        return $;
    }());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep receiving the following error when attempting to unmarshall an xml document using
I am attempting to create a simple program that will keep a text log
I'm trying to keep Visual Studio from attempting to serialize the datasource of drop
I'm attempting to build Firefox on my Windows Vista Ultimate machine. I keep getting
I am attempting to link my program executable, but I keep getting errors. Having
I am attempting to use a case statement but keep getting errors. Here's the
I am attempting to write a file to external storage in android. I keep
I am attempting to keep user inputs into our application as clean as possible.
While attempting to execute SQL insert statements using Oracle SQL Developer I keep generating
I am attempting to hash and keep(the hash) an object of type IEnumerable<anotherobject> which

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.