All the standard built-in objects in LC2 are shown below.
Array
array0[element1,element2,…]; array0[n]=element(n+1)
array1[array0,element1,…]
123456process main () {Smartisan = ["T1","T2","M1"];// define an array named "Smartisan"smartphones = [Smartisan,"iPhone","Mi"];//define an array named "smartphones"log Smartisan[0];//output T1log smartphones[0][2];//output M1}
length
length(array)
array.length
12log length(smartphones); //Output 3log smartphones.length;//Output 3
for…in/of
for(var in array)
for(var of array)
for...in/of
in instruction keyword.
123456for (x in smartphones){log x; //Output 0,1,2}for (x of smartphones){log x; //Output ["T1","T2","M1"],iPhone,Mi}
indexOf
indexOf(array,element)
indicates the position of a specified element.12log indexOf(smartphones,"Smartisan"); //Output 0log smartphones.indexOf("iPhone"); //Does not support this syntax,output -1.
Bool
bool([variable,number,RegExp])
a boolean object wrapper which could convert a variable/number/RegExp to a Boolean format.1234bool(3.14) // Output truebool(3>4) // Output falsebool(3-3) // Output falsebool(/[a-z]/) // Output true
Date
Creates a JavaScript Date instance that represents a single moment in time. Date objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC.
now
now();
returns the number of milliseconds elapsed from January 1st 1970, 00:00:00 UTC to the current time.12//Set 2017-1-1,00:00 as current timelog now(); //Output 1483200000
format
format(“time”,”display-format”)
format(now(),”display-format”)
12345678910//Output formated time.log format("May 11 2012","fullDate"); //Output:Friday,May 11,2017log format("May 11 2012","isoDate"); //Output:2012-05-11//Assuming January 1, 2017, 0:00 to be the current time. Output formated current time.log format(now(),"yyyy-mm-dd");//Output:2017-01-01log format(now(),"yyyy-m-d");//Output:2017-1-1log format(now(),"mm-d");//Output:01-1log format(now(),"dddd,mmmm dS,yyyy,h:MM:ss TT");//Output:Sunday,January 1st,2017,0:00:00 AMlog format(now(),"isoDateTime");//Output:2017-01-01T00:00+0800
Math
abs
abs([number])
returns the absolute value of a number, that is abs(x)=|x|.1234abs(-3.14) // Output 3.14abs('') // Output 0abs() // Output nullabs('string') // Output null
ceil
ceil([number])
returns the smallest integer greater than or equal to a given number.12ceil(3.14) // Output 4ceil(-3.14) // Output -3
floor
floor([number])
returns the largest integer less than or equal to a given number.12floor(3.14) // Output 3floor(-3.14) // Output -4
max
max([number1],[number2],…)
returns the maximum of a set of numbers.1max(3+2,number('3'+'2')) // Output 32
min
min([number1],[number2],…)
returns the minimum of a set of numbers.1min(3+2,number('3'+'2')) // 输出 5
random
random()
returns a float pseudo-random number in the range [0, 1), that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale it to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.12//Output a random number in a specified range.random() * (max - min) + min
Number
number(“string”)
number is a wrapper object allowing you to work with numerical values. A Number object is created using the number() constructor.123number('3.14') // Output 3.14number('3'+'2') // Output 32number('') // Output 0
String
string([variable,number])
The global object string is a constructor for a string or a sequence of characters. Values could also be converted or generated into strings with it.1log length(string(3.14)); //Output 4
length
length(“string”)
represents the length of a string.1length("Lemonce") //Output 7
charAt
charAt(“string”,[letterPosition:number])
returns the specified character from a string.Number counts from zero.1charAt('Lemonce',5) //Output c
indexOf
indexOf(“wholeString”,”chosenLetter”)
returns the index within the calling string object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.123indexOf('Lemonce','L') //Output 0indexOf('Lemonce','e') //Output 1indexOf('Lemonce','l') //Output -1
substr
substr(“string”,[startLetter:number],[length:number])
returns the characters in a string beginning at the specified location through the specified number of characters.12substr('Lemoncase',2,3) //Output 'mon'substr('Lemoncase',2) //Output 'moncase'
trim
trim(“string”)
removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).1trim(' Lemon case ') //Output 'Lemon case'