[Android-free root script production] auto.js understanding and application of grammar

        After Basics and collect the fruit a little practice, we have a rough understanding of the script, believe also had some interest in further study and the master, after all, life often perhaps spend it! Let's just say it is such a rush yesterday 618 tickets ah, ah punch from work program on small companies, and so on ...... Then you gradually learn for grammar, in order to facilitate understanding of non-IT professionals to learn, so try to talk the vernacular.

First, the use of variables

Variable name suggests, is a quantity can be changed. Simple point, can be understood as the value of letters, but there are many that value, not necessarily the numbers, decimals, there are many other types, such as characters, strings, and then a big point, a variety of objects, mobile computers , can be stored in a variable, but the data types of all kinds of objects, may contain a lot of content, and there function.

1. Create a variable - var        

var create a variable, js language is the easiest way to create. The syntax is:

var variable name; 

For example, we want to create a name for a variable, the statement is var a; then you create a variable out empty, if his output value that is null (empty), can also be created at the same time to write the initial variable value , the syntax is:

var variable name = value ;

For example, we create a value of 5, the name of the variable b, the statement is var b = 5; then use these words, the system creates a variable called b and give him an assignment, is 5, this is the digital variable data types (Number) type.

About variable names , there are certain requirements , mainly can not start with a number, variable name can not be repeated, and can not run keyword while, for, if, try the same keywords and so on. Chinese js variable name can be used, but not recommended.        

Development: at the time of creation, there is another case where we need to create a variable, but at this time, we do not know the value of this variable or if we want to define this variable there is no value, but be sure to specify data types . Then we have to use the constructor , which is a lot like the functions which have created syntax is:

var variable name = new types of data (passed value);   

We will not speak of his own class to write, how to write a constructor, take a look at how other people using the constructor of the class, for example, we want to create a data type that is a sequence, which can put a lot of the same data type as a variable (an array of arrays variable), but do not know the contents inside, this time can be used in the array constructor new array (); such statement is var d = new array (); case is created for the data type of a variable array.

At this time you should be careful of the friends discovered that the above grammar in parentheses clearly advised to write why the need to write a statement here in the incoming values are uploaded into the empty value of it? This constructor when creating the need to introduce any value, called the no-argument constructor , contrast, there have constructor parameters , such as the array, in fact, there are two constructors, there is a need incoming created out of the length of the array, for example, I want to create a length (integer type) array 5 (array which can put five variables) named array e, the statement is var e = new array (5) ; when a class has multiple constructors without parameters, it is created when the system selects a corresponding constructor according to the number and type of value you pass.

2. The basic data types        

Although there are many types of data, everyone can write your own data types. But in fact few basic data types, there are numeric (Number), boolean (Boolean), String (String), there are other unknown (Undefined), empty (null) and so on. Number Needless to say that numbers, Boolean is on or not. only two values ​​true and false, strings of words that we say abc and the like, but a string that represents the time must be taken to trap in double quotation marks in English input method, for example, to create a string variable is abc , the statement is var f = "abc"; but if you wrote var f = abc; this time, this time to run the system will look for a variable name is abc, and did not find an error will be thrown variable is not found information, cause the script to stop.

3. Variable Assignment - assignment operator =

The value of the variable, the variable is then how to modify the value of the variable it, which use the assignment operator - equal to the number. The role of this symbol is to set the variable equal sign for the values ​​after the equal sign, such as three lines in sequence

var a=5;
a=0;
log(a);

Then print variable information output is zero, although originally created a variable value of 5, but then will modify the value of the variable to 0, so the final output out or 0.

4. The variable calculation between

① string, string concatenation operation is performed with a plus sign .

With variable is the same.

between digital subtraction multiplication and division, multiplication with a *, except number with /

③ Boolean operators         

Non - exclamation mark , exclamation point effect is negated Boolean value, such as the data type had to true, with the value is false, false becomes true for the original!.        

 And - && , connecting the two boolean values, if both values are true, then it returns true, false otherwise         

Any - || , connecting the two boolean values, if there is a two is true, then the overall returns true, false otherwise give an example:

These are calculated between the return on the same data type, and returns, for example return between the two different data types, same as the true different returns false, return is calculated between different types of data, relational operators .    

① determines whether or equivalent, all of the data types can be used        

== double equal sign , it is determined whether the same two variables, connecting the two variables, if two variables are identical left and right, the overall returns true, different returns false.        

Exclamation equal number! = , It is determined whether the two different variables, connecting the two variables, if the left and right two variables, different, returns true, the same false.     

for example:

② size determination digital, used only by connecting two numbers, returns a Boolean value         greater than:> less than: <greater than or equal:> = Less than or equal: <=
 

The information output

Easy to debug the test results, we generally hit the log viewer, especially in unusual circumstances. In the information output Auto.js There are many ways, such as the console.log (); This function, would be encapsulated in Auto.js global functions, the syntax can be used directly:

log (contents of the output);

This is the most common form of output, print black to the log, usually used for general information output operation. It may be of any type, but in the end it is a form of words. Log Open Method: script editing interface -> upper right corner three points -> Log we have to test the code log ( "Monopoly"); print information it

                                                            

Click Run, then click on the upper right corner three points -> Log:

                                                            

Log output function, there are many:

console.verbose ( "to output content"); gray color is generally important to print content.

console.info ( "to output content"); color is green, the general information for printing operation.

console.warn("要输出的内容");颜色为蓝色,一般用来打印运行警报。

console.error("要输出的内容");颜色为红色,一般用来打印运行报错信息。

除了在日志里打印输出,当然还有其他方法。

气泡输出:语法:

toast("要输出内容");

对话框输出:语法:

alert("标题","内容");

6.代码注释

要是程序写长了,不要说别人,自己看,过久了都很难看懂,这时就需要在代码上标注注释,注释写详细,是写程序的一种好习惯。系统运行代码时碰到注释会自动跳过,注释只是给写代码的人看的。那么如何写注释呢。     

行级注释 :

// 注释内容

行级注释,从双斜杠开始这一行后面的,全为注释。    

块级注释:

/*注释内容*/

跨行注释,从斜杠星号一直到星号斜杠中的内容,无论多少行,都判断为注释内容。

因为过于简单,读者自己写个范例实践下就好。

 

二、判断语句的使用

1.基本if语句        

这里方法要用到if语句,基本就是

if(判断内容){运行的代码块}

一般用大括号括起来的内容,叫做一个代码块,用于放代码。判断内容是否符合,里面肯定是一个布尔值,如果true则运行后方的代码,false则不运行。举个例子:

2.if else语句        

我们需要判断一个内容如果是则运行一向内容,如果不是,运行另一向内容。这个时候就要用到if else语句。语法:

if(判断内容){true的运行代码}else{false的运行代码}

非常简单,就和下一个的例子一起看吧。

3.if-else if-else语句        

语法:

if(第一个判断内容){第一个执行代码}else if(第二个判断内容){第二个执行代码}esle{其他情况的执行代码}

运行时系统会判断第一个内容,对就执行第一个执行代码,然后结束整个if循环,不对则继续判断后面的else if后的判断内容,还是对就执行第二个,不对继续往下,以上都不对,就运行else。在第一个if和最后一个else中间的else if可以无限加,只要判断对就执行对应的执行语句,并结束整个else语句,如果不对就继续往下,一直到else。举个例子

4.switch语句        

判断一个变量,是否等于下面的值,是则运行对应的代码。语法:

举个例子:

三、循环运行的使用

脚本中通过循环可以大大减少代码,还可以完成很多本来做不到的事情。这个功能是写脚本必须学会的。循环分为好多种,比较常见的有while,for循环等。下面我们就来挨个介绍吧。

1.while循环        

while循环是最基本的一种循环用法,也是最简单的。语法是

while(判断内容){运行内容}   

运行内容在循环中,也叫循环体。判断内容如果成立,就运行循环体的内容。判断内容是布尔值,比如while(true){循环体}这样就会无限运行循环体里的内容,因为判断内容一直为true没有变过。        

比如写个示例,我要循环打印字母a到日志,那么代码为

while(true){
    log("a");
}

运行后就可以看到日志内在疯狂的打印出字母a了。如果不想运行这么快,可以在每次log之后加一个sleep进行延时,比如

while(true){
    log("a");
    sleep(1000);
}

这样就每秒打印一次a字母了。        

只是限定死循环,再实际中完全没有作用因为这样就直接进入循环运行后,下面的代码完全运行不到了。我们现在一起想,如何做出循环限定次数,我们想想要达到定量循环,那就是,达到第几遍运行时,要让判断内容不成立,这样运行到那一遍时,就运行终止了。那我们可以设置一个变量,每循环一次都增加1,然后判断是否到那和值。这个代码的具体实现方法如下:

每次运行,第七行会把i的值修改成比i大1的值,也可以用i++来直接代替该功能。当运行到第9遍时,变量i从9变到10,然后判断出10并不小于10,所以停止这次循环。另注:这一个循环里面内容共循环10次,i分别从0到9。但是每次做定量循环如此常用的功能都要这么写非常麻烦,所以就有了一种新功能for循环,详情请看下面的内容。

2.for循环的基本使用        

 for循环几乎可以说是为定量循环功能量身定做的一个功能。基本语法为

for(语句1;判断内容;语句2){循环体}   

这个语句在运行的时候系统会先运行语句1,注意,语句1只运行这一遍,不会循环,然后判断内容如果为true,就运行循环体,循环体运行完后,运行语句2,语句2运行完后就,会到判断如果为true,继续进行循环。        

直接说可能有点抽象,我们举个例子理解这个功能

查看日志结果:

3.循环语句的默认值        

for循环和while不一样,他另一个方便的地方在于他有默认值。如果while里面判断内容不写肯定会报错,而for不会。for循环语句第一个如果空着,默认不执行,第二个不输入,默认为true,即无限循环,第三个空着也默认不执行。所以for(;;){循环体内容}也可以做无限循环使用。虽然可以省略语句内容,但是for循环后面括号里的两个分号,必须不能省略,不然也会报错

4.break和continue对循环操作的使用         

在循环中,还有另一个重要的功能就是break和continue,有了这两个功能后,循环将会更加方便,和实用。        

break的作用是终止整个循环,运行了这段代码直接停止整个循环,然后当然就去运行之后的内容了。比如,我们还可以用另一个方法做定量循环:

大家可以运行后查看日志看看输出结果。当判断循环变量加到10后就运行if语句里的break,停止循环,不是10当然就不运行。当然,运行了break之后,就算当前那次循环,在break后面的语句也不会运行了。

continue的作用是结束本次循环,进入下次循环,运行了这个代码后,当前循环continue后面的代码内容都不会运行,直接进入下一次循环。我们直接来看示例:

我们通过示例结果输出可以看到,他只打印了continue前,为什么没有打印continue后的内容呢。他开始循环,运行打印continue前,然后运行到了continue,这时,进入了下一次循环,并没有运行到后面的内容,所以又运行了输出continue前,然后再进入下一次循环,等待。看了这个示例,应该能理解用法咯。

5.循环内变量的生命周期        

在循环内也可以创建变量,但是我们要是循环运行创建变量,会不会因为设置的名字相同的变量,导致报错呢,答案是不会的,因为在循环中创建的变量,当这次循环结束,系统会自动销毁这个变量。当然,因为自动销毁,下一次循环也没法调用上次循环里创建的变量。而在循环结束后,也没有办法获取循环中的变量。那我们,如果要在循环外获取到循环内处理的变量怎么办呢,那只有在循环前创建之后要获取的变量,这一个办法。使用循环前,要记得根据需求,提前设置变量咯。ps:for循环中的语句1中创建的变量是在循环前运行的,所以可以在循环后直接调用。

6.do while循环        

这个循环,是比较少用的一个循环,他的语法是:

do{循环体}while(判断内容);   

他的用法和while差不多,区别就是,他会先运行循环体内容,然后再判断内容,看看要不要继续循环运行循环体。那他和while循环的区别在哪呢。那就是,就算判断内容一开始就为false,他也会先运行一次循环体语句。循环体语句至少会运行一次。

7.setInterval循环         

这个循环的功能是定时循环,也就是每运行一次循环体,会等待一段时间。他的语法是

setInterval(function(){循环体},延时);   

循环体内容,也不用多说了。这个循环的问题在于,他并没有判断内容,也就是说,要停止这个循环只能用上面提到的break功能,后面输入的延时,是每次循环体内容运行后,等待的时间,单位是毫秒,所有程序里的时间,单位一般都是毫秒。这个功能一般比较少用,主要用处在于ui线程上的循环处理,在ui中,以上的功能,只能在新线程中运行。

 

四、数组的使用

数组。这也是到以后代码里必不可少的一部分。他的作用是同样数据类型的对象放到一个集合中,一个变量中,还可以通过下标来获取这个集合指定位置的内容。但是和他名字不同的是,在数组里放的内容不只是数,而是所有内容都可以,包括布尔值,字符串,各种对象,甚至还可以在数组里放数组(也就是高维数组)等等,这就是数组。

下面让我们来具体了解一下数组的用法吧。

1.数组的表示        

数组在程序中,用中括号表示,里面的内容用英文的逗号“,”进行分割,比如放了三个数字一到三的数组,就是[1,2,3]    这个应该很好理解,如果放了三个字符串a b c那就是["a","b","c"],ps:字符串记得加引号。

2.数组的创建         

要创建一个数组,当然可以通过直接输入初始值来创建,比如var arr=[1,2,3];    这是最基本的创建方法,那我们要创建一个空数组怎么办呢,这时要用到构造函数。就是var arr=new Array(长度);    这样可以直接创建一个指定长度的空数组,比如要创建一个长度为5的空数组:var arr=new Array(5);  还有一个构造函数,可以传入多个内容比如var arr=new Array(5,6,7,8);  这时创建出来的数组就会自动判断数据类型(js本生就是泛型)根据传入内容的数量创建,对应长度和类型的数组。打印以上例子的结果是[5,6,7,8];

3.数组的长度         

数组的长度就是数组内内容的个数,比如 [1,3,5,7,9] 这个数组,长度为5 , ['a','b','c'] 的长度为3。可以用数组的length属性获得的长度,var arr=[1,2,3];log(arr.length);这就是一个输出arr数组长度的例子,结果当然为3。

4.数组的下标        

数组的下标是获取数组中指定内容的索引,数组中的每个内容,都有一个下标,按照顺序,从0开始,[1,2,3] 这个数组的三个内容的下标分别是0,1,2。

5.数组长度与下标的关系      

  在用数组的时候有个易错点,那就是很多人会认为最后一个内容的下标,就是数组的长度。然而下标是从0开始计数,而长度是从1开始数的。具体算下来,数组最后一个内容的下标就是数组长度-1

6.数组获取指定位置的内容      

  那我们如何获取并查看修改数组内的值呢,语法是:

变量名[下标]   

就这么简单,但是要注意的是,下标是从0开始的,举个例子:

 var arr=[2,3,4,5]; 

//输出下标为2的内容,即第三个
 log(arr(2)); 
//输出信息4 

arr[0]=6; 
log(arr); 
//输出信息[6,3,4,5]; 

我们用这个方法获得到了数组里的内容后,可以直接输出,赋值给变量或是直接修改。

7.push放入内容        

push是数组的一个方法,用来朝数组内添加元素。语法是:

数组.push(放入内容);   

运行时,会先把数组最后增加一格长度,并且赋值成括号内的放入内容。非常简单,就不给例子了。

8.pop和shift删除首位        

pop和shift两个数组的方法,分别可以用来删除数组的最后一位和第一位,同时会返回删掉的内容。比如原来一个数组arr的值是[1,2,3,4],这时运行一下arr.pop();他就成[1,2,3],这时再运行arr.shift();  他的值就变成了[2,3]  用法也很简单,自己尝试吧,一般返回值的用处并不是很大。

9.split字符串分割至数组      

  我们可以通过别的类中的一些函数或者数组类中的函数,可以把数组和其他类中互相转换。比如说split就可以把字符串转换到数组,split的主要作用就是把数组通过制定字符切割成多个字符串放入一个字符串数组。比如说一个字符串

var str="abcdabcdabcd";

然后用split处理,我们用一个新的变量,

var arr=str.split("c");

那么分割出来的字符串就是ab,dab,dab,d。

如果打印输出这个数那么打印出来的内容为["ab","dab","dab","d"]

10.循环遍历数组        

我们可以用循环来对数组里的每一个内容进行操作,我们直接用一个例子来说明这个功能吧,假如我们需要对一个数字数组的每一位的值增加一,那么我们看下面那个例子。

从大体上来看这个例子,我们可以发现在每一次的循环中,都通过下标获取到了数组的某一个内容,并用我们之前讲过的双加号的方法把他的值扩大到比原来大一的值。再仔细看,每次获取的下标的大小,从第一次循环,i的初始值为0,那么数组下标为0的那一个也就是第一个,一直每次循环i++获取下标更大一的内容,一直到最后一个,之前讲过在for循环中,每次循环会判断第二格的内容是否成立,在运行下面的内容

11.高维数组        

高维数组就是数组中嵌套数组,比如说 var arr=[[1,2,3],[4,5,6],[7,8,9]]; 这就是一个数组存放了三个数字数组在里面。那我们要获取输出第二个数组中的第三个值的代码就为log(arr[1][2]);         

高维数组在多重嵌套for循环做处理常用,这里只是略讲,不过也没有什么特别的功能,很容易看懂。

 

五、面向函数式编程

什么是函数?我们也可以叫他方法,就是可以把一些常用的代码块,放到一起,然后在不同的地方都要用到的时候就可以直接调用它。这样就可以省去很多重复的代码,理论角度就可以做到没有重复的代码块。

1.创建并且调用函数:function关键字

我们知道创建变量的其中一个关键字是var,创建函数同样是有关键字的,只有一个就是function,格式代码如下:

function 代码名(){     代码块内容; }

其实很简单,比如我们要创建一个叫log123的函数,用来按顺序输出1,2,3三个数字,也就是log(1);log(2);log(3)每次打三个麻烦,于是写一个函数可以,每次调用都按顺序输出123,代码如下:

function log123(){

    log(1);

    log(2);

    log(3);

}

也就这么简单,当我们需要调用他的时候,就直接运行log123()就行了,比如 log123(); log123(); 这样子就等于调用了两遍,输出的内容应该就为:

1 2 3 1 2 3

当然如果我们不调用函数,只创建,运行这个脚本什么也不会发生的。还有另一种函数的创建方法,和变量有点类似。代码格式如下:

var 函数名 = function(){代码块内容;}

其实用法一样,同样举个上面的例子:var log123 = function(){log(1);log(2);log(3)}

/* 我们这边来讲一下上面那两种创建方法的区别,看似作用一样,其实还是有区别的,等编程到达一定境界,并且开始追求程序效率了,那就应该要知道咯。        

我们首先要知道程序是如何运行的,其实我用简单一点的话来说就是:会有解析器(等引擎)把js文件转换成机器语言(即0和1,也是设备唯一看得懂的语言),然后再让设备运行。        

这个过程中,这两种函数就会有不同的解析方式。解析器在解析的时候会先把所有函数给解析出来,也就是说,一开始解析,就会把所有第一种方式建的函数全解析出来。        

所以这两种的区别也就在程序运行的加载差别上。在面向对象编程中还会有其他问题,但正常使用中,只有非常追求性能的人可能才需要用得上。*/

2.函数返回值:return关键字         

函数可以只有运行内容,但是我们也可以让他有返回内容。比如说在我们需要获取到函数运行的结果,或者快速停止函数的时候,我们都会用到return来返回。         return的主要作用就是,停止这个函数的运行,并且返回调用他的程序 在他后面的值。我们来看个例子:

log(returnTest());

function returnTest(){

        return 123

}

这个函数的运行结果就是在日志上打印了123。他的运行效果来说很简单,而实质上是先运行了log函数,然后他去调用returnTest函数去获取返回信息,最后得到了函数返回的123,将其输出。        

当然因为js使用的是泛型所以return后面可以跟任何数据类型(字符串记得加引号),也可以返回null。在一个函数运行结束时,也可以理解为默认返回null

3.关于函数名         

关于函数名也要稍微提一下,其实函数,也算是一种数据类型,所以他的名字和变量名一样的限定规则,详细可以看前面的文章。

4.关于函数的传入参数      

  函数的传入参数非常重要,比如说你需要给函数设置一些运行参数等。就会用到传入参数。        

传入参数的方法其实就是在创建函数的地方括号里写上传入参数的名称,这个变量会在你调用的时候创建,并且赋值为传入的值。注意:只有这个函数内的程序可以调取到这个变量。这个变量会在return时被销毁。        我们具体来看一个例子吧:

t(666);

function t(num){

      log(num);

      log(num);

}

这个程序的运行结果就是会输出两次666,这个函数的作用就是输出两次传入的值(这边传入的就是666)         函数还可以传入多个参数,中间用逗号(英文半角)隔开,我们直接来看一个例子:

addLog(123,456);

function addLog(n,m){

    log(n+m)

}

我们来详细解释一下这个程序的运行吧:首先运行的是第一行,调用了这个函数,函数创建的时候也创建了变量n和m,并赋予了初始值,123和456,然后运行log函数,把123和456相加传入其中输出了579        

其实我们调用的那些全局函数等,就是可以理解为是不在我们文件中的函数,熟练运用这些函数,可以大幅减少我们程序的内容。像我们autojs可以调用所有java和android及其内部提供的函数。至少有十万甚至百万个函数。所以学习的路还很长..........

7.关于同名函数        

不同于变量,如果传入参数数量不同的话,他可以同名。而你在调用函数的时候,会根据你传入参数的个数,选择适当的函数。         我们直接举个例子:

chooseFunction(1,2)

function chooseFunction(a){

    log(a)

}

function chooseFunction(a,b){

    log(a+b)

}

至此,auto.js的语法学习完毕,还有一些关于设备等的写法可以再查看教程。

 

Guess you like

Origin blog.csdn.net/P876643136/article/details/92845958