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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:58:08+00:00 2026-06-13T06:58:08+00:00

I have this two methods : Cluster.prototype.initiate_xhr_request = function(url, callback) { var self =

  • 0

I have this two methods :

Cluster.prototype.initiate_xhr_request = function(url, callback) {
    var self = this,
        request = (Modernizr.test_xmlhttprequest) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
    request.onreadystatechange = function() {
        if(request.readyState === 4) {
            switch(request.status) {
                case 200:
                    callback.apply(request,[ request.statusText ]);
                    break;
                case 400:
                    callback.apply(request,[ request.statusText ]);
                    break;
                default:
                    break;
            };
        } else 
            console.log('An error occured during the XHR request');
        }
    };
    request.open("HEAD", url, false);
    request.send();
};

Cluster.prototype.operations = {
    '&&': function(array){ 
        return array.reduce(function(previousValue, currentValue, index, array){
             return previousValue && currentValue;
         })
     },
    '||' : function(array){
        return array.reduce(function(previousValue, currentValue, index, array){
            return this.initiate_xhr_request(previousValue) || currentValue;
        })}
};

EDIT : The methods are invoked as it follows :

Cluster.prototype.calculate = function(operation, array){
    return this.operations[operation](array);
};

/* call the calculate function */

this.calculate('&&', some_array);

Obviously, this line this.initiate_xhr_request(previousValue) won’t work. I kept trying to find a good solution of doing what I’m trying to do there, but I couldn’t find one (: Is there a way of doing that and keeping the structure I have the same ?

  • 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-13T06:58:09+00:00Added an answer on June 13, 2026 at 6:58 am

    You can use .call or .apply to manually set the this value of the method you’re invoking.

    Here I set the this value of this.operations[operation] to that in the current lexical environment. (Using .apply instead of .call would do the same, but would take your array and pass its members as individual args, which you don’t seem to want.)

    Cluster.prototype.calculate = function(operation, array){
        return this.operations[operation].call(this, array);
    };
    

    Now in those methods, the this value will be what you expect, except that you need to make sure the this value is retained in then .reduce() callback for the "||" method.

    The classic way to do that is to keep a variable reference to the outer lexical environment, an reference that variable in the callback:

    Cluster.prototype.operations = {
        '&&' : ...,
        '||' : function(array){
            var self = this;
            return array.reduce(function(previousValue, currentValue, index, array){
                return self.initiate_xhr_request(previousValue) || currentValue;
            })}
    };
    

    But a more modern way is to use Function.prototype.bind to create a new function with the this value manually bound:

    Cluster.prototype.operations = {
        '&&' : ...,
        '||' : function(array){
            return array.reduce(function(previousValue, currentValue, index, array){
                return this.initiate_xhr_request(previousValue) || currentValue;
            }.bind(this))}
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Two Methods private function cacheAdd($id,$data) { $this->minicache->set($id,$data); } private function cacheGet($id)
Possible Duplicate: Why do this Ruby object have two to_s and inspect methods that
I have a question about inheritance and template methods. Suppose I have this two
I have this two methods, and I what to put in pause this main
I have this two tables: Table A ID TYPE SDATE EDATE RATING 1 M
I have this two tables from my program to manage stock: Articulos (items): id_articulo
I have this two uls: <ul actualpage=0 id=lastcompanieslist hideable=true upperpage=1> <li page=0 style=display: none;><img
Is there any way to have this: [one[two]][three] And extract this with a regex?
Why this two functions have the strange behaviour of when I click on thead,
having this two classes I always have a doubt of which is the best

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.