WeChat applet - understanding of let, var, const

Understanding of let, var and const in WeChat applet

 

There are three ways to declare variables in JavaScript: var, let, const. 

var: Declare a global variable. In other words, the variable declared in the for loop can also be used after jumping out of the for loop.  

for(var i=0;i<=1000;i++){ var sum=0; sum+=i; } alert(sum); 

The sum declared inside the for loop can be used even if it jumps out of the for loop, and the result will pop up normally without reporting an error.

 

 let: declare block-level variables, that is, local variables. In the above example, jumping out of the for loop and using the sum variable will report an error 

Note: You must declare 'use strict' before you can use let to declare variables, otherwise the browser will not display the results 

 

const: used to declare constants, also has block-level scope const PI=3.14;

 

Also refer to :

https://blog.csdn.net/HUSHILIN001/article/details/77498774

 

Take a chestnut:

For example, using const to achieve modularization of small programs, we can extract some common code into a separate js file or a method, which is convenient to use as a module. The modularization of small programs will also be discussed later. https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/module.html

export const show = function (tip) {
  wx.showToast({
    title: tip || '成功',
    icon: 'success',
    duration: 2000
  })
}

export const showTip = function (tip) {
  wx.showModal({
    title: '提示',
    content: tip || '操作失败!',
    showCancel: false,
  })

}

 

The level is limited, if you have any questions, please leave a message to exchange.

Learn from each other and make progress together :) Please indicate the source for reprinting. Thank you.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325185249&siteId=291194637