这样直接读取obj.name属性,就能以普通调用实现async/await值,异步进程,预加载缓存
可用作异步加载非必须依赖
注意:这样的写法不能保证调取时,已经获取到数据。
var obj = {} // H_fetchApiData 高阶工厂函数,返回getApiData 请求api获取数据 let H_fetchApiData = (dynamicName) =>{ let getApiData = ()=>{ let apiData={} setTimeout(()=>{ apiData={code:1,data:1} },100) getApiData = ()=> apiData return apiData } getApiData() return getApiData } let getApiData = H_fetchApiData() Object.defineProperty(obj,'name',{ get(){ return getApiData() } }) console.log(obj.name) setTimeout(() => { console.log(obj.name) }, 200);