Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Ajax回调中,局部变量是否仍在作用域内?_Javascript_Ajax - Fatal编程技术网

Javascript 在Ajax回调中,局部变量是否仍在作用域内?

Javascript 在Ajax回调中,局部变量是否仍在作用域内?,javascript,ajax,Javascript,Ajax,一旦ajax回调函数被激活,我想调用一个内部函数。由于存在延迟,我想知道调用局部变量时是否仍然正确,特别是回调函数使用的这个.response_元素 当ajaxstatus==200 var ControlSignIn = function( ) { this.form_element = document.getElementById( 'signin' ), this.response_element = document.getElementById( 'signin_r

一旦ajax回调函数被激活,我想调用一个内部函数。由于存在延迟,我想知道调用局部变量时是否仍然正确,特别是回调函数使用的这个.response_元素

当ajax
status==200

var ControlSignIn = function( ) 
{
    this.form_element = document.getElementById( 'signin' ),
    this.response_element = document.getElementById( 'signin_response' ),
    this.text_object = new Text( this.form_element ), 
    this.message_object = new Message( this.response_element );
};

ControlSignIn.interface = function()
{
    new ControlSignIn().invoke();
};

ControlSignIn.prototype.invoke = function( ) 
{
    if( CONSTANT.VALIDATE_ON === 1 )
    {
        if( !this.text_object.checkEmpty() ) 
        {
            this.message_object.display( 'empty' );
            return false;
        }
        if( !this.text_object.checkPattern( 'email' ) ) 
        {
            this.message_object.display( 'email' );
            return false;
        }
        if( !this.text_object.checkPattern( 'pass' ) ) 
        {
            this.message_object.display( 'pass' );
            return false;
        }
    }

/* new internal call_back */    

        AjaxNew.use( ajaxSerialize( this.form_element ) + '&ajax_type=ControlSignIn', ControlSignIn.invoke.callBack );

    function callBack( server_response_text )
    {
        ajaxType( server_response_text, this.response_element, 'respond' ); 
    }

/*  Removed
    Ajax.repeatUse( ajaxSerialize( this.form_element ) + '&ajax_type=ControlSignIn', ajaxTypeRespond, this.response_element );
*/

};
解决方案:

AjaxNew.use( ajaxSerialize( this.form_element ) + '&ajax_type=ControlSignIn', ControlSignIn.invoke.callBack );

var response_element = this.response_element;
function callBack( server_response_text )
{
    ajaxType( server_response_text, response_element, 'respond' ); 
}

答案是“是”和“否”。是的,局部变量仍然存在,但每次函数调用时,
引用总是被重置。因此,回调将有自己的
这个

解决方案是利用答案中的“是”部分,并将您的
引用保存在另一个变量中。然后在回调中使用该变量(而不是
this


现在,也就是说,你的代码结构让我有点困惑,我不能确切地告诉你的“回调”应该如何工作;也就是说,它引用了
这个
,但我不知道它希望如何设置它。

不,它不会。即使属性可能完好无损,
不会指向对象

将值复制到局部变量,并使用函数文字,则局部变量将被捕获到函数的闭包中,并保持完整且可访问:

var element = this.response_element;
AjaxNew.use(
  ajaxSerialize(this.form_element) + '&ajax_type=ControlSignIn',
  function(server_response_text) {
    ajaxType(server_response_text, element, 'respond'); 
});

您可能正在考虑使用
someFunction.call(这个应该是什么,arg,arg…)