箭头功能与功能关键字

时间:2019-05-06 19:35:25

标签: javascript function arrow-functions

我有这个箭头功能,可以正常工作。

var sendData = (method, url, data, callback = null) => {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = callback;
    xhttp.open(method, url, true);
    xhttp.send(JSON.stringify(data));
};

但是,如果回调是这样定义的

sendDataDebugCallback = () => {
    if (this.readyState == 4 && this.status == 200) {
        console.log('Hi');
        console.log(this.responseText);
    }
};

它不起作用,但是如果我使用function关键字,它会起作用

function sendDataDebugCallback() {
    if (this.readyState == 4 && this.status == 200) {
        console.log('Hi');
        console.log(this.responseText);
    }
};

这两种函数声明之间有什么区别?为什么箭头功能不起作用?

0 个答案:

没有答案
相关问题