Js 的回调方法怎么写啊

AAA.js

let bbb = new BBB();
bbb.setCallback(this.response);
...
response: function(message) {
    cc.log(message);
},

BBB.js

properties: {
    response : null,
},

setCallback: function (re){
    this.response = re;
}

...
this.response(message);

最后报错 Connect.js:63 Uncaught TypeError: this.response is not a function
at XMLHttpRequest.xhr.onreadystatechange

估计又是this作用域的问题,把this.response(message);调用前后的代码发一下吧~

确实是this 作用域问题.
BBB.js

    sendHttp: function (command, dto, callback) {
        this.response = callback;

        var xhr = cc.loader.getXMLHttpRequest();
        this.httpResponse(xhr);
        ...
        xhr.send(dto.toString());
    },

    httpResponse: function ( xhr, responseHandler ) {
        var handler = responseHandler || function (response) {
            return "POST" + " Response (30 chars): " + response.substring(0, 30) + "...";
        };
        
        // Simple events
        ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'].forEach(function (eventname) {
            xhr["on" + eventname] = function () {
                cc.log("eventname : " + eventname);
            };
        });
    
        // Special event
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
                this.response(xhr.responseText);
            } else {
                //cc.log(xhr.responseText);
            }
        };
    }

但不知道这个位置怎么写.

已经解决, 我用一个成员属性来保存this, 不知道大神有没有更好的方法.

Connect.instance = this;
...
Connect.instance.response(xhr.responseText);

对的,需要保存this,或者使用箭头函数:

var self = this;
xhr.onreadystatechange = function () {
    self.response...
}
// 或者
xhr.onreadystatechange = () => {
    this.response...
}
--------------------------------------------------
// 上面的箭头函数会被Creator内置的Babel编译成下面这样
// 所以在只支持ES5的引擎里本质上和第一种是一样的
// 只不过写起来比较方便
var _this = this;
xhr.onreadystatechange = function () {
    _this.response...
}

不知道你写没写过c++,(我一般用c++的回调方式)
我一般在回调的时候,参数会传一个target,func,两个,
调用时,target.func()