In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge about why WeChat Mini Programs uses var that=this. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Code example:
Page ({data: {test:10}, testfun1: function () {console.log (this.data.test) / / 10 function testfun2 () {console.log (this.data.test) / / undefined} testfun2 ()},})
The first this.data.test prints a result of 10 because the this points to the Page object that contains the custom function testfun1 ().
The second print statement will actually report an error, because in the function testfun2 (), the this point has changed, and there is no data attribute, so it will error:Cannot read property 'data' of undefined
The solution is to copy a copy of the this pointing to the variable, so that although the this changes during function execution, the that still points to the previous object.
Testfun1: function () {var that = this console.log (this.data.test) / / 10 function testfun2 () {console.log (that.data.test) / / 10} testfun2 ()}, onLoad:function () {this.testfun1 ();}
No error is reported after compilation, and the result is printed normally.
Let's take a clearer example:
OnLoad: function () {var testvar = {name: "zxin", testfun3: function () {console.log (this.name);}} testvar.testfun3 ();}
Output result after compilation: zxin. This.name refers to the testvar object, and testfun3 () also belongs to the testvar object.
These are all the contents of the article "Why do you use var that=this in WeChat Mini Programs?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.