Turn: literal syntax features in Javascript

Source: Internet
Author: User

Transferred from

Guan shixuan

Http://www.cnblogs.com/birdshome/archive/2005/02/26/105867.html

 

Literal syntax in Javascript

 

Common JS data types include number, Boolean, String, array, function, and object. Here, the number, Boolean, and string types are simple, and text writing is their basic method.NewThe definition of XXX () will make people think that it is a fart.

Number: VaR I =   100 ; I =   100.11 ;
Boolean: VaR B =   True ; B =   False ;
String: VaR Str = ' This Is a string .';

What should we do for complex data types, functions, arrays, and objects? Functions are defined in text. Let's take a look at how arrays and objects are represented. Suppose we have an array:

VaR Ary =   New Array ( 6 );
Ary [ 0 ] =   Null ;
Ary [ 1 ] =   1 ;
Ary [ 2 ] = 'String ';
Ary [ 3 ] =   True ;
Ary [ 4 ] =   Function ()
{
Return 'Kekeke ';
};
Ary [ 5 ] =   New Myobject ();

We use the text method (that is, the initialization method we usually call) to write this array. It will be:

VaR Ary1 = [ Null , 1 , 'String ', True , Function (){ Return 'Kekeke ';}, New Myobject ()];

Is it much simpler than above? In addition, the array regionalization method can be far more complex than this method, for example:

VaR Ary2 = []; // Empty array, equivalent to new array ();
VaRAry3 = [ 1 ,[ 2 ,[ 3 ,[ 4 ,[ 5 ,[ 6 ,[ 7 ,[ 8 ,[ 9 ,[ 0 ];

What is the third ary3 array? I don't know @_@.

No, how come ary [5] isNewWhat about myobject? Oh, sorry, let's take the myobject example again, if it is defined:

Function Myobject ()
{
This . Properties1 =   1 ;
This . Properties2 = ' 2 ';
This . Properties3 = [ 3 ];
This . Tostring =   Function ()
{
Return '[Class myobject]';
};
}

Myobject. Prototype. Method1 =   Function ()
{
Return   This . Properties1 +   This . Properties3 [ 0 ];
};

Myobject. Prototype. method2 =   Function ()
{
Return   This . Properties2;
};

So ourVaROBJ =NewHow is myobject () normalized? In fact, it is quite simple. The regionalization definition of obj is as follows:

VaR OBJ =  
{
Properties1: 1 , Properties2 :' 2 ', Properties3 :[ 3 ],
Method1: Function (){ Return   This . Properties1 +   This . Properties3 [ 0 ] ;},
Method2: Function (){ Return   This . Preperties2 ;}
};


Although the direct regionalization definition of this class instance is not simplified, It is not bad. In this way, we can replaceNewMyobject. The syntax defined by class instance regionalization is to use a pair of "{}" to represent the class, that is, "{}" is equivalent"NewObject ()". Then "{}" organizes attributes and methods by "key: value", the key can be any [A-Za-z0-9 _] character combination, or even the beginning of the number is legal @_@, value is any legal regionalized JavaScript data, and each key-value pair is separated.

 

------------------------------------------------------------------

Appendix:

In JavaScript, an object is actually an array, so you can use the array assignment syntax. :)

Some objects can traverse member using the following methods:

For (var obj in object ){

Alert (OBJ );

}

 

For (var obj in object ){

Alert (object [OBJ]);

}

You can see the value of each attribute, and you can also see itsCode.

 

 

 

 

Related Article

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.