小程序中闭包问题

微信小程序,微信公开课,小程序中闭包问题 微信公开课

微信公开课小程序热门讨论关于「小程序中闭包问题」的小程序开发问题
补充:varuser=TheGlobal;varobj={user:Theobject,getUser:function(){returnfunction(){returnthisuser;}}};Page({页面的初始数据data:{},btn_run:function(){console
展开:varuser=TheGlobal;varobj={user:Theobject,getUser:function(){returnfunction(){returnthisuser;}}};Page({** *页面的初始数据 *data:{},btn_run:function(){consolelog(objgetUser()());}})如上所述代码,在闭包中使用this对象调用obj对象中的user属性时,会报名undifine错误这是为什么VM1646:1thirdScriptErrorCannotreadpropertyuserofundefined;atpagesindexindexpagebtn_runfunctionTypeError:Cannotreadpropertyuserofundefined athttp:127001:63456appservicepagesindexindexjs:8:18 atebtn_run(http:127001:63456appservicepagesindexindexjs:21:30) ate<anonymous(http:127001:63456appservice__dev__WAServicejs:18:4907) atea(http:127001:63456appservice__dev__WAServicejs:17:31002) atz(http:127001:63456appservice__dev__WAServicejs:17:24941) atFunction<anonymous(http:127001:63456appservice__dev__WAServicejs:17:26562) athttp:127001:63456appservice__dev__WAServicejs:18:509 athttp:127001:63456appservice__dev__WAServicejs:6:26768 ate(anonymousfunction)(http:127001:63456appservice__dev__WAServicejs:4:4917) ata(http:127001:63456appserviceappservice:1008:9451)即速小程序:小程序的页面内的全局变量,应该是需要放在data里的可以去了解一下vuejs跟小程序的js还是比较相通的:可以参考一下javascript中this的用法:请问这里是在干嘛,看不懂你在写什么,能讲解一下吗varobj={user:Theobject,getUser:function(){returnfunction(){returnthisuser;}}};
更多有关「小程序中闭包问题」的讨论可以前往微信公开课小程序参与互动哦!了解更多

「小程序中闭包问题」热门讨论订阅

微信小程序,微信公开课,小程序中闭包问题
订阅小程序

方法1:微信扫描微信公开课小程序码即可订阅热门讨论「小程序中闭包问题」

方法2:微信搜索微信公开课小程序名称进入,即可订阅热门讨论「小程序中闭包问题」

方法3:微信网页访问即速商店,长按识别微信公开课小程序码即可订阅热门讨论「小程序中闭包问题」

微信公开课小程序热门讨论「小程序中闭包问题」由微信公开课原创摘录于微信小程序商店shop.jisuapp.cn,转载请注明出处。

微信公开课热门讨论「小程序中闭包问题」由微信公开课开发者向微信用户提供,并对本服务内容、数据资料及其运营行为等真实性、合法性及有效性承担全部责任。