1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-28 21:32:20 +00:00

Use function assign instead of function declaration inner function

在函数内定义函数推荐使用变量赋值方式代替函数声明方式
This commit is contained in:
waiting 2017-11-08 17:41:35 +08:00
parent 6b300cf491
commit 08a623935e
No known key found for this signature in database
GPG Key ID: 2C7023E12106AFFF

View File

@ -118,14 +118,7 @@ function loadImageAsync(url) {
```javascript ```javascript
const getJSON = function(url) { const getJSON = function(url) {
const promise = new Promise(function(resolve, reject){ const promise = new Promise(function(resolve, reject){
const client = new XMLHttpRequest(); const handler = function () {
client.open("GET", url);
client.onreadystatechange = handler;
client.responseType = "json";
client.setRequestHeader("Accept", "application/json");
client.send();
function handler() {
if (this.readyState !== 4) { if (this.readyState !== 4) {
return; return;
} }
@ -135,6 +128,13 @@ const getJSON = function(url) {
reject(new Error(this.statusText)); reject(new Error(this.statusText));
} }
}; };
const client = new XMLHttpRequest();
client.open("GET", url);
client.onreadystatechange = handler;
client.responseType = "json";
client.setRequestHeader("Accept", "application/json");
client.send();
}); });
return promise; return promise;