LiveScript 字面量

 
 

The LiveScript Book

字面值

Numbers

.4是被视为无效的,你必须使用前导0。例如:0.4

1.2016
2.12.34
3.0.4

数字之间的下划线和数值后追加的字母将自动被忽略。

1.12_000yuan

等价于

1.12000;

可以使用base~number来表示236进制数字

1.6~12
2.2~1000
3.16~ff

翻译后是这样子的

1.8;
2.8;
3.255;

Booleans, Void, Null

别名跟 CoffeeScript 中的一样

1.true
2.false
3.on
4.off
5.yes
6.no

然后,结果如下

1.true;
2.false;
3.true;
4.false;
5.true;
6.false;

JavaScript 中,undefined是可以被重定义的,所以总是使用void操作符来产生undefined是一种高明的做法。

1.void
2.x = void
3.x
1.
2.var x;
3.x = void 8;
4.x;

Strings

你可以使用单引号或者双引号来表示一个字符串

1.'a string'
2."a string"
1.'a string';
2."a string";

字符串也可以使用前置反斜杠来替代单引号,但是不能包含, ; ] ) } 或者是空白格

1.word
2.func word, word
3.(func word)
4.[word]
5.{pro: word}

JavaScript 是这样的

1.'word';
2.func('word', 'word');
3.func('word');
4.['word'];
5.({
6. pro: 'word'
7.});

双引号字符串允许插入,然而单引号是不可以的,简单的变量插入是可以省略掉花括号的。

1."The answer is #{2 + 2}"
2.'As #{is}'
3.
4.variable = word
5."hello #variable"
1.var variable;
2."The answer is " + (2 + 2);
3.'As #{is}';
4.variable = 'word';
5."hello " + variable;

在你进行插入操作的字符串前使用%,将以数组的形式返回字符串的原始部分,这样就可以按你想的那样去连接字符了。

1.%"#x*#y+#z"
1.[x, "*", y, "+", z];

注释

单行注释是以 # 开头的,并且不会保留到编译后的 JavaScript 源码中去,
多行注释使用 /*注释*/,会将注释保留到编译后的结果中去

对象

花括号是可选的。

1.obj = {prop: 1, thing: moo}
2.
3.person =
4. age: 23
5. eye-color: green
6. height: 180cm
7.
8.oneline = color: lue, heat: 4
1.var obj, person, oneline;
2.obj = {
3. prop: 1,
4. thing: 'moo'
5.};
6.person = {
7. age: 23,
8. eyeColor: 'green',
9. height: 180
10.};
11.oneline = {
12. color: 'blue',
13. heat: 4
14.};

动态关键字。

1.obj =
2. "#{variable}": 23
3. (person.eye-color): false
1.var obj, ref$;
2.obj = (ref$ = {}, ref$[variable + ""] = 23, ref$[person.eyeColor] = false, ref$);

如果你的属性名和变量名一样,你可以采用更快捷的方法。

1.x = 1
2.y = 2
3.obj = {x, z: y}
1.var x, y, obj;
2.x = 1;
3.y = 2;
4.obj = {
5. x: x,
6. z: y
7.};

布尔型成员属性快速设置:

1.{+debug, -live}
1.({
2. debug: true,
3. live: false
4.});

没必要再使用this.去访问成员了,我们可以使用@来表示。

1.this
2.@
3.@location
1.this;
2.this;
3.this.location;

正则表达式

正则表达式使用单个/来表示。

1./moo/gi
1./moo/gi;

使用//可以包含多行,评论,以及空格

1.//
2.| [!=]==? # equality
3.| @@ # constructor
4.| <[(?:[sS]*?]>)? # words
5.//g

1./|[!=]==?|@@|<[(?:[sS]*?]>)?/g;

列表

通常表字面量使用中括号描述:

1.[ 1, person.age, 'Chinese Food' ]
1.[ 1, person.age, 'Chinese Food' ];

如果表中的项目是不可以被调用的,那么逗号是可以省略的:

1.[ 1 2 3 true word 'hello here' ]
1.[1, 2, 3, true, 'word', 'hello here'];

使用缩进语句块来创建隐式表。至少需要两个项目才行。如果你只有一个项目,你可以使用...来强制它建立隐式表:

1.my-list =
2. 32 + 1
3. person.height
4. 'beautiful'
5.
6.one-item =
7. 1
8. ...
1.var myList, oneItem;
2.myList = [32 + 1, person.height, 'beautiful'];
3.oneItem = [1];

当创建隐式表时,可以使用*来消除歧义,*并不包含到表内,仅仅是拿来避免与其他的表混淆用的。

1.tree =
2. * 1
3. * 2
4. 3
5. 4
6. * 5
7. 6
8. * 7
9. 8
10. * 9
11. 10
12. 11
13.
14.obj-list =
15. * name: 'tessa'
16. age: 23
17. * name: 'kendall'
18. age: 23
19.
20.obj-one-list =
21. * name: 'tessa'
22. age: 23
23. ...
1.var tree, objList, objOneList;
2.tree = [
3. [1, [2, 3], 4],
4. [5, 6, [7, 8, [9, 10]], 11]
5.];
6.objList = [{
7. name: 'tessa',
8. age: 23
9.}, {
10. name: 'kendall',
11. age: 23
12.}];
13.objOneList = [{
14. name: 'tessa',
15. age: 23
16.}];

单词表:

1.<[ list of words ]>
1.['list', 'of', 'words'];

范围

to表示少于等于这个数值。til表示少于这个数值。

你也可以使用by来指定步长。如果你省略第一个数值,将被默认为0

1.[ 1 to 5 ]            #=> [ 1, 2, 3, 4, 5]
2.[ 1 til 5 ] #=> [ 1, 2, 3, 4]
3.[ 1 to 10 by 2 ] #=> [ 1, 3, 7, 9 ]
4.[ 4 to 1 ] #=> [ 4, 3, 2, 1 ]
5.[ to 5 ] #=> [ 0, 1, 2, 3, 4, 5 ]
6.[ A to D ] #=> [ 'A', 'B', 'C', 'D' ]

如果你的范围中包含表达式,并且想要降序产生一个数组列表,那么必须显示指定by:

1.x = 4
2.[ 1 to x ] #=> [ 1, 2, 3, 4 ]
3.[ x to 0 by -1 ] #=> [ 4, 3, 2, 1, 0 ]

Misc

用于循环嵌套时的标签:

1.:label 4 + 2
1.label: {
2. 4 + 2;
3.}

constructor简写:

1.@@
2.@@x
3.x@@y
1.constructor;
2.constructor.x;
3.x.constructor.y;

占位符:


  1....
 
原文地址:https://www.cnblogs.com/crackpotisback/p/5170107.html