Small Program scripting language Wxs detailed

Source: Internet
Author: User

Wxs scripting language is the abbreviation for Weixin script scripts, which is the internal file type of JS, JSON, Wxml, WXSS, and then a large program. As of now, the applet has provided 5 file types.

Several ways to deconstruct small programs, one of which is through file types: JS, JSON, Wxml, WXSS, WXS. The previous four kinds have been introduced before, today is mainly introduced WXS this type of file. If you are a PHP, JAVA, C # Programmer to write the server-side code, it is very good to understand wxs this scripting language. It is more convenient to dynamically implement some basic logic judgments on the page, instead of relying on all the background implementations, and then returning through the interface.

First, Wxs creation method
There are two ways to use WXS. One is written directly in the Wxml

Ii. Composition of WXS documents
1, the data type is similar to the small program building bricks, warm bricks, precast parts and other content, these are common content, these are the basic components of programming language, the data type can be simple type, can be complex type. From ordinary numbers to complex objects.

There are currently 8 types of data in the WXS language:
Number: Value string: String Boolean: Boolean object: Object function: Function array: array Date: Date regexp: Regular

Using Var or let to define variables in Wxs, it is difficult to see the type of a variable from the definition and determine what type a variable is. You can use the variable's property constructor. You can also use typeof this way to determine the type of the variable
For example:

var number = 10;console.log( "Number" === number.constructor );console.log( ‘number‘ === typeof number );

2, variable variable is the building of small program the basic pieces of this building, although it is very small, but has its own specifications, mainly the following aspects: Naming specification, the first character must be: letter (a-za-z), underscore (), the remaining characters can be: letter (a-za-z), Underline (), Number (0-9). The different case is different variable
Reserved characters

delete void typeof null undefined NaN Infinity var if else true false require this function arguments return for while do break continue switch casedefault

3, operators according to the number of operands, can be divided into one yuan, two yuan, multivariate operators. The subtraction that we use all require two operands to complete, which is the two-dollar operator. Self-increment or decrement these require only one operator to complete. These are unary operators. It is a multivariate operator that requires more than two operands.

4, the Choice structure choice structure, also known as the judgment structure, if the C condition satisfies the execution one kind of circumstance, the condition does not satisfy then executes another kind of situation. The other is the multi-conditional, if the C1 condition satisfies, then executes part A, if the C2 condition satisfies then executes part B, if the C3 condition satisfies then holds the C part, and so on, all conditions do not satisfy is to execute the z part.

// if ... else if (表达式) 语句;else 语句;if (表达式) 语句;else 语句;if (表达式) { 代码块; } else { 代码块; }// if ... else if ... else ...if (表达式) { 代码块; } else if (表达式) { 代码块; } else if (表达式) { 代码块; } else { 代码块; }switch (表达式) {  case 变量:  语句;  case 数字:  语句;  break;  case 字符串:  语句;  default:  语句; }

5, circular structure simple things, do not want to repeat the code, it can be the public execution of the partial stripping out

for (语句; 语句; 语句) 语句;for (语句; 语句; 语句) { 代码块; }while (表达式) 语句;while (表达式){ 代码块; }do { 代码块; } while (表达式)

Supports the use of break,continue keywords. Break is the end of the entire loop continue is interrupting the current loop
When we get here, we'll stop. Components completed through the two parts of 1 and 2, and then 3, 4, 5 of the three most of the "binder" can be completed after 6 and 7 slowly you will find that writing small program is the same as building a house, the raw materials ready, the use of adhesives to connect to it.
6. Module (custom function) write business logic and content in Wxs, and use moudle.exports to expose variables or functions.

// /pages/tools.wxsvar foo = "‘hello world‘ from tools.wxs";var bar = function (d) { return d; }module.exports = {  FOO: foo,  bar: bar,};module.exports.msg = "some msg";在使用的地方地方,需要引入对就的模块才能使用<wxs src="./../tools.wxs" module="tools" /> {{tools.msg}} view> {{tools.bar(tools.FOO)}} view>

Note: tool. Foo Foo is capitalized because the above section is a variable name that is exposed as a public using Foo.
7. Function library (functions already defined)

consoleconsole.log() console.info() console.warn() console.error() 这是引用的chrome的经典内容。MathMath.PI Math.ceil() Math.floor()带有()是对应的方法,没有带括号的是属性,NumberMAX_VALUEMIN_VALUENEGATIVE_INFINITYPOSITIVE_INFINITYJSONstringify(object): 将 object 对象转换为 JSON 字符串,并返回该字符串。parse(string): 将 JSON 字符串转化成对象,并返回该对象。Global属性NaNInfinityundefined方法parseIntparseFloatisNaNisFinitedecodeURIdecodeURIComponentencodeURIencodeURIComponent

More usage of the above content can be referred to ES syntax http://es6.ruanyifeng.com/

Third, comments
Separate this piece out, on the one hand is to explain its importance, on the other hand is to learn a language and other people in a very important part of communication. WXS There are 3 main ways to annotate.

module= "Sample" >

Method One: single-line comment

/* Method Two: Multi-line Comment */

/* Method Three: end note. That is, all WXS codes from/* onwards are commented

var a = 1;

var B = 2;

var c = "fake";

In programming practice, as long as the idea can be cleared to realize the code, in Big Bro's company basically recommended is to write the comments first, and then the comment content into code.

Load page data//Add value to comment area//number of rotten eggs//number of forwards//votes

So for everyone to learn a new language, do not be afraid, as long as you can express their own ideas, and then one step at a time to the annotation content into code language can, through our previous learning debugging tools to complete their own small program.

In summary: Depending on the needs of the different data types by the way the variable name, by manipulating operators to combine into their own desired business code.

Related article: Wxs interpretation of new functions of small programs
Article Source: Small Program Alliance

Small Program scripting language Wxs detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.