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.