Home >Web Front-end >JS Tutorial >Literal Syntax feature analysis in JavaScript language_javascript skills

Literal Syntax feature analysis in JavaScript language_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:17:41944browse

我刚开始学习JavaScript的时候,老是会被JavaScript里的一些莫名其妙的语法形式搞的糊里糊涂的。而且也不知道到底它支持了多少那些莫名其妙的语法形式?现在通过这个几个月的深入了解,算是把它们弄得比较清楚了,所以下面就来说说JS的Literal Syntax特性。

    JavaScript语言的文本化的特性?My God,难道有哪门语言的编写不是使用文本吗?不使用文本还能用啥?用意识流吗?真是faint哈。

    虽然我们的每种语言都使用文本来表示,说远点asm是文本的吧,BASIC是文本的吧,C/C++、C#、java啥的都是文本吧。这没有错,它们的语言表达都是文本形式,可是它们却不能使用文本来表示所有内容。简单类型比如数字啊字符串啊还行,但是C#或Java可以用文本表示一个对象实例吗(不是用一堆定义语句,然后再new个对象实例哦)?显然是不行的,然而JavaScript却提供了对所有数据类型包括复杂对象的文本化书写方式。

    我们编写JS常用的数据类型包括:Number、Boolean、String、Array、Function和Object。其中这里面的Number、Boolean和String属于简单类型,文字方式书写是它们的基本方式,如果用new xxx()来定义反而会让人觉得是脱裤子打屁。 

Literal Syntax feature analysis in JavaScript language_javascript skills Number: var i = 100; i = 100.11;
Literal Syntax feature analysis in JavaScript language_javascript skills Boolean: 
var b = true; b = false;
Literal Syntax feature analysis in JavaScript language_javascript skills String: 
var str = 'this is a string.';


    对于复杂数据类型,函数、数组和对象我们怎么办呢?函数不用说了,都是以文本方式定义的。下面就看看数组和对象是怎么表示的吧。假如我们有一个数组:

Literal Syntax feature analysis in JavaScript language_javascript skills var ary = new Array(6);
Literal Syntax feature analysis in JavaScript language_javascript skills ary[
0= null;
Literal Syntax feature analysis in JavaScript language_javascript skills ary[
1= 1;
Literal Syntax feature analysis in JavaScript language_javascript skills ary[
2= 'string';
Literal Syntax feature analysis in JavaScript language_javascript skills ary[
3= true;
Literal Syntax feature analysis in JavaScript language_javascript skills ary[
4= function()
Literal Syntax feature analysis in JavaScript language_javascript skills {
Literal Syntax feature analysis in JavaScript language_javascript skills     
return 'keke';
Literal Syntax feature analysis in JavaScript language_javascript skills };
Literal Syntax feature analysis in JavaScript language_javascript skills ary[
5= new MyObject();


    我们使用文本方式(也就是我们平时说的初始化方式)来写这个数组它将是:

Literal Syntax feature analysis in JavaScript language_javascript skills var ary1 = [null1, 'string', truefunction(){return 'keke';}, new MyObject()];Literal Syntax feature analysis in JavaScript language_javascript skills


    比上面精简的多吧?而且这里数组的文本化方式还可以写的远比这复杂的多,比如:

   var ary2 = []; //空数组,相当于 new Array();
   var ary3 = [1,[2,[3,[4,[5,[6,[7,[8,[9,[0]]]]]]]]]];


    第三个ary3是啥数组,我也不知道了@_@。

    不对呀,怎么ary[5]是new MyObject()呢?哦,不好意思,我们再来把MyObject示例一下,假如它被定义为:

Literal Syntax feature analysis in JavaScript language_javascript skills function MyObject()
Literal Syntax feature analysis in JavaScript language_javascript skills {
Literal Syntax feature analysis in JavaScript language_javascript skills     
this.Properties1 = 1;
Literal Syntax feature analysis in JavaScript language_javascript skills     
this.Properties2 = '2';
Literal Syntax feature analysis in JavaScript language_javascript skills     
this.Properties3 = [3];
Literal Syntax feature analysis in JavaScript language_javascript skills    
this.toString = function()
Literal Syntax feature analysis in JavaScript language_javascript skills     {
Literal Syntax feature analysis in JavaScript language_javascript skills          
return '[class MyObject]';
Literal Syntax feature analysis in JavaScript language_javascript skills     };
Literal Syntax feature analysis in JavaScript language_javascript skills }
Literal Syntax feature analysis in JavaScript language_javascript skills
Literal Syntax feature analysis in JavaScript language_javascript skills MyObject.prototype.Method1 
= function()
Literal Syntax feature analysis in JavaScript language_javascript skills {
Literal Syntax feature analysis in JavaScript language_javascript skills     
return this.Properties1 + this.Properties3[0];
Literal Syntax feature analysis in JavaScript language_javascript skills };
Literal Syntax feature analysis in JavaScript language_javascript skills
Literal Syntax feature analysis in JavaScript language_javascript skills MyObject.prototype.Method2 
= function()
Literal Syntax feature analysis in JavaScript language_javascript skills {
Literal Syntax feature analysis in JavaScript language_javascript skills     
return this.Properties2;
Literal Syntax feature analysis in JavaScript language_javascript skills };Literal Syntax feature analysis in JavaScript language_javascript skills


So how do we text our var obj = new MyObject()? In fact, it is very simple. The textual definition of obj is as follows:

Literal Syntax feature analysis in JavaScript language_javascript skills var obj =
Literal Syntax feature analysis in JavaScript language_javascript skills {
Literal Syntax feature analysis in JavaScript language_javascript skills Properties1 :
1, Properties2 : '2', Properties3 : [3],
Literal Syntax feature analysis in JavaScript language_javascript skills Method1:
function(){ return this.Properties1 this.Properties3[0];},
Literal Syntax feature analysis in JavaScript language_javascript skills Method2:
function(){ return this.Preperties2; }
Literal Syntax feature analysis in JavaScript language_javascript skills };


Although the direct textual definition of this class instance is not streamlined, it is not bad. In this way, we can use this textual class instance to replace the new MyObject() in ary. The syntax for textual definition of class instances is to use a pair of "{}" to represent the class, which means that "{}" is completely equivalent to "new Object()". Then press "key:value" within "{}" to organize properties and methods. Key can be any combination of characters [A-Za-z0-9_], even numbers starting with @_@ are legal, and value is any legal Textual JavaScript data, and finally each key-value pair is separated by ",".

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn