变量

概念

  • WXS 中的变量均为值的引用。
  • 没有声明的变量直接赋值使用,会被定义为全局变量。
  • 如果只声明变量而不赋值,则默认值为 undefined。
  • var表现与javascript一致,会有变量提升。
    1. var foo = 1;
    2. var bar = "hello world";
    3. var i; // i === undefined

上面代码,分别声明了 foobari 三个变量。然后,foo 赋值为数值 1bar 赋值为字符串 "hello world"

变量名

变量命名必须符合下面两个规则:

  • 首字符必须是:字母(a-zA-Z),下划线(_)
  • 剩余字符可以是:字母(a-zA-Z),下划线(_), 数字(0-9)

    保留标识符

以下标识符不能作为变量名:

  1. delete
  2. void
  3. typeof
  4. null
  5. undefined
  6. NaN
  7. Infinity
  8. var
  9. if
  10. else
  11. true
  12. false
  13. require
  14. this
  15. function
  16. arguments
  17. return
  18. for
  19. while
  20. do
  21. break
  22. continue
  23. switch
  24. case
  25. default

原文:

https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxs/02variate.html