From 08a623935ecc7bb35f1419a40c3f2eff98fac1de Mon Sep 17 00:00:00 2001 From: waiting <1661926154@qq.com> Date: Wed, 8 Nov 2017 17:41:35 +0800 Subject: [PATCH] Use function assign instead of function declaration inner function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在函数内定义函数推荐使用变量赋值方式代替函数声明方式 --- docs/promise.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/promise.md b/docs/promise.md index 7ea9e42..78ae462 100644 --- a/docs/promise.md +++ b/docs/promise.md @@ -118,14 +118,7 @@ function loadImageAsync(url) { ```javascript const getJSON = function(url) { const promise = new Promise(function(resolve, reject){ - const client = new XMLHttpRequest(); - client.open("GET", url); - client.onreadystatechange = handler; - client.responseType = "json"; - client.setRequestHeader("Accept", "application/json"); - client.send(); - - function handler() { + const handler = function () { if (this.readyState !== 4) { return; } @@ -135,6 +128,13 @@ const getJSON = function(url) { 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;