Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.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对象内执行回调函数_Javascript_Callback_Scope_Javascript Objects - Fatal编程技术网

在javascript对象内执行回调函数

在javascript对象内执行回调函数,javascript,callback,scope,javascript-objects,Javascript,Callback,Scope,Javascript Objects,我想在对象内部执行回调函数。我不知道我这样做是否有什么不对劲 我在谷歌上搜索了一个解决方案,也在stackoverflow上搜索过,但找不到任何类似于我编写代码的方法 PHPGateway.js var PHPGateway = { opt_friendlyURL: true, opt_folder: 'ajax/', callback_function: null, useFriendlyURL: function (bool) { thi

我想在对象内部执行回调函数。我不知道我这样做是否有什么不对劲

我在谷歌上搜索了一个解决方案,也在stackoverflow上搜索过,但找不到任何类似于我编写代码的方法

PHPGateway.js

var PHPGateway = {

    opt_friendlyURL: true,
    opt_folder: 'ajax/',

    callback_function: null,

    useFriendlyURL: function (bool) {
        this.opt_friendlyURL = bool;
    },

    setFolder: function (folder) {
        this.opt_folder = folder;
    },

    send: function (service, method, data, callback) {
        var url,
            json_data = {};
        if (this.opt_friendlyURL) {
            url = this.opt_folder + service + '/' + method;
        } else {
            url = this.opt_folder + 'gateway.php?c=' + service + '&m=' + method;
        }
        if (data != undefined) {
            json_data = JSON.stringify(data);
        }
        this.callback_function = (callback == undefined) ? null : callback;
        $.ajax({
            method: 'POST',
            url: url,
            data: {data: json_data},
            success: this.ajax_success,
            error: this.ajax_error
        });
    },

    ajax_success: function (returned_object) {
        if (this.callback_function != null) {
            this.callback_function(returned_object.error, returned_object.data);
        }
    },

    ajax_error: function () {
        this.callback_function.call(false, {});
    }

};
然后在加载PHPGateway.js的HTML文件中,我有以下代码:

<script>
    function submit_handler(event) {
        event.preventDefault();
        form_submit();
    }
    function form_callback(error, data) {
        if(error == null) {
            alert(data.text);
        }
    }
    function form_submit() {
        var data = {
            status: $('#inStatus').val(),
            amount: $('#inAmount').val(),
            id: $('#inBudgetID'). val()
        }
        PHPGateway.send('budget', 'status', data, form_callback);
    }
    $('form').one('submit', submit_handler);
</script>
现在它工作了

这是你的问题:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    success: this.ajax_success,
    error: this.ajax_error
});
当您将
success
error
设置为
this
上的方法时,它们不会保留其
this
。当调用JavaScript函数时,它会绑定一个
this

someFunction(); // this is undefined or the global object, depending on strict
someObject.someFunction(); // this is someObject
函数
对象的内置
.call
.apply
.bind
可帮助您覆盖此功能

在您的例子中,我认为jQuery将
this
绑定到Ajax对象——这是不使用jQuery和始终使用严格模式的一个很好的理由

如果您可以保证或填充ES5支持,
bind
是一个简单的解决方案:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    success: this.ajax_success.bind(this),
    error: this.ajax_error.bind(this)
});
现在,给你一个提示:不要使用名称空间,如果你这样做了,不要使用
this
<代码>此对于要构造的对象非常有用。如果你真的必须这样做,那么更合适的做法是:

var PHPGateway = (function() {
    var callbackFunction;
    var options = {
        friendlyURL: true,
        …
    };
    …

    function send(service, method, data, callback) {
        …
    }
    …

    return { send: send };
})();
这是你的问题:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    success: this.ajax_success,
    error: this.ajax_error
});
当您将
success
error
设置为
this
上的方法时,它们不会保留其
this
。当调用JavaScript函数时,它会绑定一个
this

someFunction(); // this is undefined or the global object, depending on strict
someObject.someFunction(); // this is someObject
函数
对象的内置
.call
.apply
.bind
可帮助您覆盖此功能

在您的例子中,我认为jQuery将
this
绑定到Ajax对象——这是不使用jQuery和始终使用严格模式的一个很好的理由

如果您可以保证或填充ES5支持,
bind
是一个简单的解决方案:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    success: this.ajax_success.bind(this),
    error: this.ajax_error.bind(this)
});
现在,给你一个提示:不要使用名称空间,如果你这样做了,不要使用
this
<代码>此对于要构造的对象非常有用。如果你真的必须这样做,那么更合适的做法是:

var PHPGateway = (function() {
    var callbackFunction;
    var options = {
        friendlyURL: true,
        …
    };
    …

    function send(service, method, data, callback) {
        …
    }
    …

    return { send: send };
})();
这是你的问题:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    success: this.ajax_success,
    error: this.ajax_error
});
当您将
success
error
设置为
this
上的方法时,它们不会保留其
this
。当调用JavaScript函数时,它会绑定一个
this

someFunction(); // this is undefined or the global object, depending on strict
someObject.someFunction(); // this is someObject
函数
对象的内置
.call
.apply
.bind
可帮助您覆盖此功能

在您的例子中,我认为jQuery将
this
绑定到Ajax对象——这是不使用jQuery和始终使用严格模式的一个很好的理由

如果您可以保证或填充ES5支持,
bind
是一个简单的解决方案:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    success: this.ajax_success.bind(this),
    error: this.ajax_error.bind(this)
});
现在,给你一个提示:不要使用名称空间,如果你这样做了,不要使用
this
<代码>此对于要构造的对象非常有用。如果你真的必须这样做,那么更合适的做法是:

var PHPGateway = (function() {
    var callbackFunction;
    var options = {
        friendlyURL: true,
        …
    };
    …

    function send(service, method, data, callback) {
        …
    }
    …

    return { send: send };
})();
这是你的问题:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    success: this.ajax_success,
    error: this.ajax_error
});
当您将
success
error
设置为
this
上的方法时,它们不会保留其
this
。当调用JavaScript函数时,它会绑定一个
this

someFunction(); // this is undefined or the global object, depending on strict
someObject.someFunction(); // this is someObject
函数
对象的内置
.call
.apply
.bind
可帮助您覆盖此功能

在您的例子中,我认为jQuery将
this
绑定到Ajax对象——这是不使用jQuery和始终使用严格模式的一个很好的理由

如果您可以保证或填充ES5支持,
bind
是一个简单的解决方案:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    success: this.ajax_success.bind(this),
    error: this.ajax_error.bind(this)
});
现在,给你一个提示:不要使用名称空间,如果你这样做了,不要使用
this
<代码>此对于要构造的对象非常有用。如果你真的必须这样做,那么更合适的做法是:

var PHPGateway = (function() {
    var callbackFunction;
    var options = {
        friendlyURL: true,
        …
    };
    …

    function send(service, method, data, callback) {
        …
    }
    …

    return { send: send };
})();

在对$.ajax的调用中,您需要添加一个
context
选项:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    context: this,
    success: this.ajax_success,
    error: this.ajax_error
});

Ajax成功和错误处理程序中的
这个
变量没有指向您认为是的对象。$.ajax()的
上下文
选项设置此
在ajax回调中指向哪个对象。

在对$.ajax的调用中,您需要添加一个
上下文
选项:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    context: this,
    success: this.ajax_success,
    error: this.ajax_error
});

Ajax成功和错误处理程序中的
这个
变量没有指向您认为是的对象。$.ajax()的
上下文
选项设置此
在ajax回调中指向哪个对象。

在对$.ajax的调用中,您需要添加一个
上下文
选项:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    context: this,
    success: this.ajax_success,
    error: this.ajax_error
});

Ajax成功和错误处理程序中的
这个
变量没有指向您认为是的对象。$.ajax()的
上下文
选项设置此
在ajax回调中指向哪个对象。

在对$.ajax的调用中,您需要添加一个
上下文
选项:

$.ajax({
    method: 'POST',
    url: url,
    data: {data: json_data},
    context: this,
    success: this.ajax_success,
    error: this.ajax_error
});


Ajax成功和错误处理程序中的
这个
变量没有指向您认为是的对象。$.ajax()的
context
选项设置此
对象在ajax回调中指向哪个对象。

这对我来说似乎很好。这个代码是在其他代码中间吗?是的,这很好。你的问题在别的地方。至于基于意见的“最佳方式”,这应该很好。您是否正在使用另一个对象的方法,如
myCallbackFunction
,调用时会抛出类似的错误,但不是同一个错误?(或者可能是同一个,而你实际上在做某种递归的事情?)这是可行的,但相当奇怪?你有一个空属性,在函数中你用一个外部函数覆盖这个属性,这个外部函数甚至没有从对象中调用,你可以调用回调,
callback('Hello World!')
,它会完全一样吗?等等,是的,这实际上是不可能的(除非你有一个