One person develops a product, applet from 0 to 1, Chapter 3 framework syntax

Time:2020-10-27

Many resumes have “proficient” in the word, but the basic knowledge is not qualified. It is suggested that the comrades who study programming should be patient and start from frame grammar. Otherwise, one day, you still have to learn from frame grammar.

JS of wechat applet is a set of script language, combined with wxml, can build the structure of the page. It is a different language from JavaScript and has its own syntax. The syntax content includes keywords, comments, output, variables, etc.

3.1 key words

Keywords can be used to indicate the beginning or end of a control statement, or to perform specific operations, etc. Keywords are language reserved and cannot be used as identifiers. For example, variable names cannot use the keyword VaR and constant names cannot use const. Keywords in development tools, commonly used bright colors, let you fascinated.

delete void typeof null undefined NaN Infinity

var let const if else true false

require this function arguments return for while

do break continue switch case default

3.2 notes

There are single line comments starting with / / and double line comments starting with / * * / ending with / * * /. Annotations are for developers, not machines. Programmers who don’t take notes as programming tasks are all hooligans.

/** 
   *Execute at the end of the first rendering
   *A page is executed only once 
   */
  onReady: function() {
    //Read initial value
  }

3.3 output

In the JS file, you can use the console.log (VaR) output information in the console, convenient for developers to call JS code, find code related problems, it is a high frequency function. Var variable, which can be array type, object type or string type, can accept one or more parameters and output them together.

1. Usage

use console.log () when one or more variable values are output, spaces are automatically added between the values and a newline character is automatically added at the end of each output.

No more nonsense. Code is the hard core. Open pages / index/ index.js File, content transformation.

//index.js
//Get application instance
const app = getApp()

Page({
  data: {
    motto: 'Hello World'
  },
  //Event handler
  bindViewTap: function() {},
  onLoad: function() {
    console.log ('I have a very nice name:','ximen Chuishui ');
    console.log ('I don't know martial arts, I can only blow water');
  }
})

Output content

I have a very nice name: Ximen Chuishui
I don't know martial arts. I can only blow water

2. Place holder

stay console.log (), you can process and output information through some special placeholders to make the output more in line with your personalized requirements. The supported placeholders are: string (% s), integer (% d or% I), floating point number (% f), object (% o) and style (% C).

onLoad: function() {
    console.log ('ximen% C blowing% C water ',' color:green ', ' color:red ).
    console.log ('very difficult% s arithmetic','addition ');
    console.log('%d + %d =', 1, 1, 2);
}

One person develops a product, applet from 0 to 1, Chapter 3 framework syntax

3. Special methods

In development, when you need to count the execution time of a piece of code, you can use the console.time Method and start time marking console.timeEnd Method marks the end time and outputs the number of milliseconds passed between the end time and the start time in the console.

During development, if there is a need to group data information, it can be used console.group The () method is used to set the starting position of grouping information console.groupEnd The () method is used to end the current grouping as follows.

onLoad: function() {
    console.time (the "run time");
    console.group (the "first set of information");
    console.log (group 1, line 1);
    console.log (group 1, line 2);
    console.groupEnd();
    console.group (the "second set of information");
    console.log (group 2, line 1);
    console.log (line 2 of group 2);
    console.log (group 2, line 3);
    console.groupEnd();
    console.timeEnd (the "run time");
  }

One person develops a product, applet from 0 to 1, Chapter 3 framework syntax

4. More output

In the JS file, in addition to the console.log () output information for JS code debugging http://onsole.info () and console.debug () two good friends. They are essentially related to console.log () it doesn’t make much difference, it’s just another name.

In addition, the console.warn () output warning (prompt) information with a yellow icon in front of the output log console.error () outputs an error message with a red icon in front of the output log.

onLoad: function() {
    console.warn ('I don't know what to suggest ');
    console.error ('you're wrong? My fault? -- no, it's all about the code. )
  }

One person develops a product, applet from 0 to 1, Chapter 3 framework syntax

There are several others, which are rarely used. I don’t need to talk about them. If I have time, I’d better study the content of the next section: constant variables.