Analysis of literal syntax characteristics in JavaScript language _javascript skills

Source: Internet
Author: User
When I first started learning JavaScript, I was always confused by some inexplicable grammatical forms in javascript. And I don't know how many of those inexplicable grammatical forms it supports? Now through this a few months of in-depth understanding, is to make them more clear, so the following is to say JS literal syntax characteristics.

The textual nature of the JavaScript language? My God, is there any language that is not written using text? What can I use without text? With the stream of consciousness? It's really faint, huh?

Although each of our languages use text to express, say far point asm is the text of the bar, Basic is the text of the bar, C/S, C #, Java What is the text bar. This is not wrong, their language expression is the text form, but they cannot use the text to represent all content. Simple types such as numbers and strings are fine, but can C # or Java represent an object instance in text (not with a bunch of definition statements and then new object instances?)? Obviously not, but JavaScript provides a textual way of writing all data types, including complex objects.

We write JS commonly used data types include: number, Boolean, String, Array, function, and object. Where the number, Boolean, and string belong to a simple type, writing is the basic way to do it, if you use NewXXX () to define instead will let a person feel to take off pants to fart.
Number : var i = i = 100.11;
Boolean: var b = true; b = false;
String: var str = 'is a string. ';


For complex data types, functions, arrays, and objects What do we do? Functions are not to mention, they are defined in textual terms. Let's look at how arrays and objects are represented. If we have an array:

Varary= NewArray (6);
ary[0]=Null;
ary[ 1 ]  =   1 ;
 ary[ 2 ]  =   ' string ';
 ary[ 3 ]  =   true ;
 ary[ 4 ]  =   function ()
 {
return   ' Keke ';
 };
 ary[ 5 ]  =   New  myobject (); , [n], "():" ()


We're going to write this array using the text (which is our usual way of initializing) and it will be:

var ary1 = [null, 1, ' string ', true, function() {return ' Keke ';}, new MyObject ()];


More streamlined than it is? And the textual way of the array can be written far more complex than this, such as:

var ary2 = []; //empty array, equivalent to new array ();
var ary3 = [1, [2, [3, [4,[5, [6, [7] , [8, [9, [0] ]]];


The third ary3 is an array, and I don't know @_@.

No, how Ary[5] is new MyObject ()? Oh, I'm sorry, let's take the MyObject example, if it's defined as:

functionMyObject ()
{
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;
 }; /span>


So our var obj = new MyObject () How to text it? In fact, it's very simple, and the text of obj is defined as follows:

VarObj=
{
Properties1: 1 , properties2 :  ' 2 ',  properties3 : [ 3 ],
         method1 :   function () {  return   this . properties1  +   this . properties3[ 0 ];
          method2 :  function () {  return   this . PREPERTIES2;&NBSP}
    };

The direct textual definition of the


    instance of this class, while not concise, is not bad. This allows us to replace the ary new  myobject () with this textual class instance. The syntax for the literal definition of a class instance is to represent the class with a pair of "{}", which means that "{}" is completely equivalent to the new Object (). Then "{}" within the "Key:value" organizational properties and methods, the key can be any [a-za-z0-9_] character combination, or even the beginning of the number is legitimate @_@,value is any legitimate text JavaScript data, and finally each key value pairs with "," To separate the line.

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.