-4

You can call normal functions or constructor functions like this:

fun.apply(this);
fun.call(this);
fun.bind(this); fun();

but if function is DOM object constructor function, how do you call it remotely and pass this.

One example would be XMLhttpRequest.

Make it work like XMLhttpRequest.apply(etc);

I am trying to make constructor function that not only initialize a new object with Dom Object Constructor but also add extra stuff that i want it to have.

for example:

function myxmlhttpfunc () {
     this = new XMLhttpRequest();
     this.myprop = 'etc';
}

But as you can try or see 2nd line wont work, i tried using apply,call,bind. Only way to get it do that is return new XMLhttpRequest(); which overrides myprop. If there is a way to execute multiple statements at return i'd appreciate it. I am even considering calling settimeout, but trying to avoid it. What i'd do is pass this as a reference to time out once its initialized by return then define new properties as i like.

Muhammad Umer
  • 2,228
  • 2
  • 18
  • 17

1 Answers1

1

Just make a wrapper, it is impossible to inherit from XHR because the methods are hard-coded to only work on legitimate XHR objects - I.E. they are non-generic.

function MyXhr() {
    this.prop = "asd";
    this.xhr = new XMLHttpRequest();
}
var method = MyXhr.prototype;

//Define all the standard methods to work on `this.xhr`
//Example
method.open = function( method, url, async, user, password ) {
     if( this.prop !== "asd" ) {
         throw new Error( "Cannot open if prop is not 'asd'" );
     }
     return this.xhr.open( method, url, async, user, password );
};

Here's is basically what is going on inside the built-in methods and why nothing will work:

function XMLHttpRequest() {
     ...
}

XMLHttpRequest.prototype.open = function() {
     //ALL methods do this check which is why nothing can work:
     if( !( this is true object of XMLHttpRequest ) ) {
        throw new Error("Invalid invocation");
     }
};

Btw if you are ok with augmenting, then you can do:

function MyXhr() {
    var ret = new XMLHttpRequest();
    ret.prop = "asd";
    ret.newMethod = function(){};
    return ret;
}

This way functions are not inherited but that could easily not matter for something like XHR

Esailija
  • 138,174
  • 23
  • 272
  • 326
  • "the methods are hard-coded to only work on legitimate XHR objects" ...well i found that out after 3 hours of hell and before posting here. After long tryings, i thought maybe i'd put it in proto like this `this.__proto__= new XMLHttpRequest();` property like open showed up but didn't work. – Muhammad Umer Aug 04 '13 at 23:10
  • @MuhammadUmer yes that doesn't work because the methods only work on XHR objects like I said so the only thing that will work is a wrapper like I suggested... trust me – Esailija Aug 04 '13 at 23:13
  • 1
    @MuhammadUmer let me try to explain it in my answer – Esailija Aug 04 '13 at 23:17
  • After spending 6+ hours. I have done it. I wanted to modify the `onreadystate` function as so that no matter where its defined it also kept my function with it. I had to learn 3 techniques, and used i think 4 techniques while tried like 9 techniques. proto,apply,call,bind, wont work. Mysteriously if you redefine using define property it wont be called either. http://jsfiddle.net/techsin/hAsP7/14/ – Muhammad Umer Aug 05 '13 at 03:14
  • @MuhammadUmer you should have said in your question what you wanted to do (monkey patch onreadystatechange setter), would have saved you a lot of time I think :P [Classic XY problem](http://mywiki.wooledge.org/XyProblem) – Esailija Aug 05 '13 at 03:59
  • @MuhammadUmer [monkey **patch**](http://en.wikipedia.org/wiki/Monkey_patch)... you don't have to know the exact term to describe it... I just use it because it's shorter to say in comments.. When you were asked what problem you wanted to solve the problem was "I want my code to run when someone anywhere sets `onreadystatechange`", that's not too long to explain. – Esailija Aug 06 '13 at 07:47
  • next time: but from what i have seen on here...if you ask for something that is not easy or possible your question gets wiped out so soon that maybe if there was a chance of someone who knew of stumbling upon it is gone faster than you can blink. So yea, i was cautious. Oh i am blocked here again. i am running out of email addresses to use here. – Muhammad Umer Aug 06 '13 at 07:49
  • but your answer helped a lot though, it confirmed my hunch that such was the case. :D – Muhammad Umer Aug 06 '13 at 07:51
  • @MuhammadUmer I find that is only true when something is not explained in clear terms... *always* take all questions in comments seriously and add more detail to your question before it gets closed. – Esailija Aug 06 '13 at 07:51
  • @MuhammadUmer consider this question http://stackoverflow.com/q/7775767/995876 exactly the same problem (with `.open` instead of `.onreadystatechange`) but he explains the problem directly... no downvotes or closure – Esailija Aug 06 '13 at 07:53
  • SO..should make it that down votes can be given till at least 3 hours. If you ask and comeback after only 22 min. Question + account is gone. Not always but sometimes, critical times. – Muhammad Umer Aug 06 '13 at 07:54
  • @MuhammadUmer the FAQ specifically says not to go away when asking a question because the most activitity is in the first 5 minutes and you must be there to answer all questions about detail. – Esailija Aug 06 '13 at 07:55
  • `open` is easier. But yea..i wasn't sure what to ask for, but next time ill be sure to write paragraph. Is there youtube video by so explaining there rules fast. – Muhammad Umer Aug 06 '13 at 07:56
  • @MuhammadUmer not a video but see http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx http://catb.org/esr/faqs/smart-questions.html http://sscce.org/ – Esailija Aug 06 '13 at 07:59