functionfetchData(path,handler){varxmlHttp=newXMLHttpRequest();varresult={};xmlHttp.open("GET",path,false);xmlHttp.send(null);result=JSON.parse(xmlHttp.responseText);handler(result.data);}functionshowResult(result){console.log('The result is: '+result);}varpath='http://www.json-generator.com/api/json/get/bPQMSaHjsi?indent=2';fetchData(path,showResult);// The result is: Hello, World!
functioncurriedFetchData(path){varxmlHttp=newXMLHttpRequest();varresult={};xmlHttp.open("GET",path,false);xmlHttp.send(null);result=JSON.parse(xmlHttp.responseText);returnfunction(_callback){_callback(result.data);}}functionshowResult(result){console.log('The result is: '+result);}varpath='http://www.json-generator.com/api/json/get/bPQMSaHjsi?indent=2';// 用法一
vargetData=curriedFetchData(path)(showResult);getData(showResult);// The result is: Hello, World!
// 用法二
curriedFetchData(path)(showResult);// The result is: Hello, World!