English 中文(简体)
功能原型定义中的 Java字标的约束力问题
原标题:Javascript object binding problem inside of function prototype definitions

我正试图指出对功能原型进行约束的适当位置,以便日后称作。 例子的全文见:

http://www.iprosites.com/jso/

我的 j夫例子非常基本:

function Obj(width, height){
    this.width = width;
    this.height = height;
}

Obj.prototype.test = function(){
    var xhr=init();
    xhr.open( GET ,  ?ajax=test , true);
    xhr.setRequestHeader( Content-Type ,  application/x-www-form-urlencoded; charset=UTF-8 );
    xhr.onreadystatechange = function() {
        if (xhr.responseText ==  403 ) {
            window.location.reload(false);
        }
        if (xhr.readyState == 4 && xhr.status == 200) {
            this.response = parseResponse(xhr.responseText);
            document.getElementById( resp ).innerHTML = this.response.return_value;
            this.doAnotherAction();
        }
    };
    xhr.send();
}

Obj.prototype.doAnotherAction = function(){
    alert( Another Action Done );
}


var myo = new Obj(4, 6);

如果你试图在大火中进行原状测试,你就会得到“这种行动不是功能”。 测试中可发现2项辅助职能(即)和4项。 j 如果你想看到这些联系,但不应与这一问题太相关。 我申明这一点。 反其他行动认为,“这”是指从试验中预期的XMLHttpResponse物体。

谁能以有约束力的方式帮助了解方向? 我所尝试的一切似乎都不奏效!

我确实使用Mootools,尽管图书馆没有出席这一榜样。

提前感谢,

Arion

问题回答

。 Obj 。 您需要在当地变量中捕获this,然后在<代码>xhr.onreadystatechange内加以使用。 i.e.

Obj.prototype.test = function(){
    var obj = this,
        xhr=init();
    xhr.open( GET ,  ?ajax=test , true);
    xhr.setRequestHeader( Content-Type ,  application/x-www-form-urlencoded; charset=UTF-8 );
    xhr.onreadystatechange = function() {
        if (xhr.responseText ==  403 ) {
            window.location.reload(false);
        }
        if (xhr.readyState == 4 && xhr.status == 200) {
            this.response = parseResponse(xhr.responseText);
            document.getElementById( resp ).innerHTML = this.response.return_value;
            obj.doAnotherAction();
        }
    };
    xhr.send();
}

在你测试页上通过将上述内容贴上ole,然后运行myo.test():

欧研中心第5版已经从原型框架中借用了bind的功能方法。 如果尚未确定约束方法,你可以将其列入你的法典。

if (!Function.prototype.bind)
{
    Function.prototype.bind = function() {
        var fn = this, 
            args = Array.prototype.slice.call(arguments), 
            object = args.shift();

        return function() {
            return fn.apply(object,
                args.concat(Array.prototype.slice.call(arguments)));
        };
    };
}

一旦确定,你可在<条码>内对匿名功能进行约束。

xhr.onreadystatechange = function() {
    ...
}.bind(this);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签