Original: Lee warfare (leadzen). Shenzhen 2008-2-23



Programming the world exist only two basic elements, one is data, a code. Programming world is in the data and code in a web of tangled displays unlimited vigor and vitality.

Data is inherently quiet, with a total want to maintain their inherent qualities; and code are naturally lively, with a total want to change the world.

You see, the relationship between the data source of energy and material relations between the surprisingly similar. Data also have inertia, if there is no code to exert external force, she always maintain its original state. The code is like energy, he's the sole purpose of existence is to try to change the original state data. Code change data at the same time, also because of the resistance data and in turn affect or change the code the original trend. Even in some cases, data can be transformed into code, and code it may be transformed into data, may also exist a similar form of E = MC2 equation does the digital conversion. However, that is, in the data and code that the contradictions between such a unified operation, always reflect the law of the computer world, these laws is exactly what we prepared logic.

However, because different programmers have different world view, these data and the code will look different. Thus, different view of the world's programmers to use their own methodology, to promote the evolution of the programming world and development.

As we all know, today's most popular programming idea is the idea of object-oriented programming. Why is the idea of object-oriented programming quickly swept the world? Since the first idea of object-oriented data and code put into the continuum, and the concept of a simple object presented to the programmer. This all of a sudden those who will be the original algorithm and subroutine clutter, as well as the tangled complexity of data structure, divided into clear and orderly structure of the object, thereby clarifying the data and code in our hearts that乱麻Mission knot-like. We can have a more clear thinking, high up on another idea to explore a more vast programming world.

Legend taught at弘忍End "object真经" after day, he had his disciples of the public said: "has just been finished, presumably Seoul sentiment should be, please look at their respective偈子个Writing." Disciples are to be神秀big U.S. public think the most savvy senior, his偈子wrote: "who are the object tree, such as type of heart as prescribed.朝朝ground dust, Do not can the dust alight!."此偈an immediate sensation caused师兄弟们, everyone says that written wonderful. Monk Huineng only look after the flame and gently sighed and picked at the wall wrote: "This object has no roots, the type is also invisible. Originally, no one objects, where can the dust alight?." Then shook his head, left. The U.S. saw Huineng偈子say: "What is written in a Mess of the ah, can not read."弘忍master read poetry神秀also nodded his head mechanic praise poems Huineng look after the mechanic shook his head in silence. On the same day at night, has quietly put Huineng弘忍called its own Buddhist temple, the software will treasure for many years to teach in真经him, then let him take advantage of the moonlight night to escape ...

Later, the master trust Huineng really high hopes, in the South created a Zen another broad sky. Huineng was taken away and the software there are a真经are "JavaScript真经"!

Simple regression

To understand JavaScript, you must first put aside the concept of objects and classes and return to the primitive data and code. As mentioned above, programming the world's only data and code are two basic elements, and these two elements also have a tangled relationship. JavaScript is to put data and code are simplified to the most primitive level.

The data in JavaScript is very simple. Easy data only undefined, null, boolean, number and string these five, and complex data, only one, namely, object. This is like Chinese classical simple materialist thought, put the most basic elements of the world classified as金木水火土, and other complex substances by these five basic elements.

JavaScript code is only manifested in a form that function.

Note: The above words are all lowercase, do not and Number, String, Object, Function built-in functions, such as JavaScript confused. Need to know, JavaScript language is case-sensitive you!

Any one of JavaScript's logo, constants, variables and parameters are only unfined, null, bool, number, string, object and function types, it is typeof return value indicates the type. In addition there are no other types of.

First to give you easy data types.

undefined: on behalf of all unknown things, nothing, can not imagine the code it will not be able to handle the go.
Note: typeof (undefined) return is undefined.
Undefined can be assigned to any variable or property, but this does not mean that the removal of the variable, it will therefore have one more property.

null: it has a concept, but nothing. None appear to have, there are also no. Although hard to imagine, but can be used to deal with the code.
Note: typeof (null) return object, but null is not the object, with a null value of the variable is not object.

boolean: yes that is, non-non, there is no doubt. On the right, it would be wrong, absolutely clear. Code can be deal with, but also can control the code flow.

number: linear things, size, and the order is clear, many without chaos. Code to facilitate batch processing, but also control code and the iterative cycle.
Note: typeof (NaN) and typeof (Infinity) are back number.
NaN participate in any numerical calculation of the structure are NaN, and NaN! = NaN. Infinity / Infinity = NaN.

string: the rational thing for human beings, not machines signal. Man-machine communication, code accordingly to understand people's intentions and so on, have been relied.

Simple types are not objects, JavaScript objects do not confer the ability of these simple types. Directly be given the type of easy money often identifiers, variables and parameters is not an object.

The so-called "object oriented", that is, data and code can be organized into complex structures. JavaScript, only the type of object type and function to provide the target capacity.

No category

object is the type of object. Medium in JavaScript, no matter how complex data and code can be organized into object form of the object.

However, JavaScript has no "type" concept!

For many object-oriented programmers, this is probably the most difficult to understand JavaScript place. Ah yes, almost any object-oriented book stresses, the first want to say is the "type" concept, this is the pillar of object-oriented. This sudden lack of a "category", we did not like all of a sudden spiritual pillars are六神无主. It appears that objects and classes to put aside to achieve "the target of this non-root, type also invisible" realm is indeed not be easy ah.

In this way, we first take a look at JavaScript for some procedures:

Js code

  1. var life = ();
  2. for (life.age = 1; life.age <= 3; life.age + +)
  3. (
  4. switch (life.age)
  5. (
  6. case 1: life.body = "egg";
  7. life.say = function () (alert (this. age + this. body));
  8. break;
  9. case 2: life.tail = "tail";
  10. life.gill = "gills";
  11. life.body = "tadpole";
  12. life.say = function () (alert (this. age + this. body +"-"+ this. tail +","+ this. gill));
  13. break;
  14. case 3: delete life.tail;
  15. delete life.gill;
  16. life.legs = "four legs";
  17. life.lung = "lung";
  18. life.body = "frog";
  19. life.say = function () (alert (this. age + this. body +"-"+ this. legs +","+ this. lung));
  20. break;
  21. );
  22. life.say ();
  23. );

  var life = {};
    for(life.age = 1; life.age <= 3; life.age++)
    {
        switch(life.age)
        {
            case 1: life.body = "卵细胞";
                    life.say = function(){alert(this.age+this.body)};
                    break;
            case 2: life.tail = "尾巴";
                    life.gill = "腮";
                    life.body = "蝌蚪";
                    life.say = function(){alert(this.age+this.body+"-"+this.tail+","+this.gill)};
                    break;
            case 3: delete life.tail;
                    delete life.gill;
                    life.legs = "四条腿";
                    life.lung = "肺";
                    life.body = "青蛙";
                    life.say = function(){alert(this.age+this.body+"-"+this.legs+","+this.lung)};
                    break;
        };
        life.say();
    };

JavaScript this procedure resulted in a life-object life, life was born just a naked object, there is no property and methods. At first the life course, there is a property of physical body, and have a say methods, appears to be an "egg." Life in the second process, it sprouted a "tail" and "gills", with tail and gill property, it is clear that it is a "tadpole." At the third time in the life course, and its tail and gill property disappeared, but grow the "four legs" and "lungs", with the legs and lung property, which eventually turned into a "frog." If you, your imagination, then perhaps it will turn into handsome "Prince", married a beautiful "princess" or something. However, after watching this program, please consider a question:

We must need to type it?

Still remember childhood that "little tadpole looking for Mother," the fairy tale it? Yesterday may be late, your child happens to be in this beautiful fairy to sleep in the bar. Cute small tadpole that is, at its own type of continuous evolution, has gradually become the mother and the same "category" in order to find its own mother. This fairy tale contains programming philosophy is this: the object "category" from scratch are also evolving, they finally disappear into the ...

"Class" can indeed help us to understand the complexity of the real world, this confusion of the real world is indeed to be classified. However, if the U.S. thought was, "category" locking, and "Class" will become "tired." Imagine, if a life that began when the object has been provided for a fixed "category", it also the evolution of it? Tadpoles become frogs can it? Can also give the children a small tadpole-speaking mother to find the story?

Therefore, JavaScript does not "category", category has been based on the intangible, and object integration. It was put aside because of the "type" concept, JavaScript object only other programming languages do not have the vitality.

If, when you began to have deep insights, you have gradually begun to understand the Zen of JavaScript.

Function of the magic

Next, we discuss the magic of your JavaScript function.

JavaScript code can only function as a form, function is the function type. There may be other programming language code such as procedure or method, but only function in JavaScript form. When we write a function of time, just set up a function only types of entities. See the following procedures:

Java code

  1. function myfunc ()
  2. (
  3. alert ( "hello");
  4. );
  5. alert (typeof (myfunc));

   function myfunc()
    {
        alert("hello");
    };
    
    alert(typeof(myfunc));

This code can be seen running after the typeof (myfunc) are the return function. More than a function of writing we call "the definition of" type of, if we be rewritten into the following "variable" type of, even easier to understand:

Js code

  1. var myfunc = function ()
  2. (
  3. alert ( "hello");
  4. );
  5. alert (typeof (myfunc));

   var myfunc = function ()
        {
            alert("hello");
        };
    
    alert(typeof(myfunc));

Here a clear definition of a variable myfunc, of its initial value is given a function of the entity. Therefore, typeof (myfunc) is the return function. In fact, the wording of these two functions are equivalent, except that the nuances of its internal implementation is identical. In other words, we write these JavaScript function is only an order of the names of variables only, the variable type is the function, the variable's value is that we prepare a function of the code body.

Clever you may immediately be further asked: Now that function is variable, then the variables can be arbitrarily assigned and used in place of arbitrary Hello?

We take a look at the following code:

Js code

  1. var myfunc = function ()
  2. (
  3. alert ( "hello");
  4. );
  5. myfunc (); / / first call myfunc, output hello
  6. myfunc = function ()
  7. (
  8. alert ( "yeah");
  9. );
  10. myfunc (); / / second call myfunc, the output yeah

   var myfunc = function ()
        {
            alert("hello");
        };
    myfunc(); //第一次调用myfunc,输出hello
    
    myfunc = function ()
        {
            alert("yeah");
        };    
    myfunc(); //第二次调用myfunc,将输出yeah

The results of running this process tell us: the answer is yes! At first after the function call, function variables has been given a new function code body, making the second call to this function when a different output.

Well, we again put the above code into a first-style function definition of the form:

Js code

  1. function myfunc ()
  2. (
  3. alert ( "hello");
  4. );
  5. myfunc (); / / here call myfunc, rather than output yeah hello
  6. function myfunc ()
  7. (
  8. alert ( "yeah");
  9. );
  10. myfunc (); / / here call myfunc, the output of course yeah

 function myfunc ()
    {
        alert("hello");
    };
    myfunc(); //这里调用myfunc,输出yeah而不是hello
    
    function myfunc ()
    {
        alert("yeah");
    };    
    myfunc(); //这里调用myfunc,当然输出yeah

Reason, the two signatures exactly the same function, in other programming languages should be illegal. However, in JavaScript, this is true. However, the program runs after they found a strange phenomenon: twice to call that function only in the final output value! Obviously the first function does not play any role. This is Why?

The original, JavaScript execution engine is not a line by line analysis and implementation of procedures, but for some period of analysis of the implementation. Moreover, in the same paragraph, the analysis of the implementation procedures, the definition of a function of statement type will be extracted to give priority to implementing. After the implementation of function definition, before any other statement the order code. In other words, at first before calling myfunc, the first statement the definition of function code logic, has been the second statement covers the function definition. Therefore, both the implementation of the final call is a function of logic.

If you put the JavaScript code is divided into two sections, for example, they write in a html, and use <script/> this tag will be divided into two:

Js code

  1. <script>
  2. function myfunc ()
  3. (
  4. alert ( "hello");
  5. );
  6. myfunc (); / / here call myfunc, output hello
  7. </ script>
  8. <script>
  9. function myfunc ()
  10. (
  11. alert ( "yeah");
  12. );
  13. myfunc (); / / here call myfunc, output yeah
  14. </ script>

<script>
    function myfunc ()
    {
        alert("hello");
    };
    myfunc(); //这里调用myfunc,输出hello
</script>

<script>
    function myfunc ()
    {
        alert("yeah");
    };    
    myfunc(); //这里调用myfunc,输出yeah
</script>

At this time, the output is in accordance with their respective order, and it proves that indeed is a JavaScript implementation of the paragraph.

The definition of a section of code-type function would give priority to the implementation of statements, it seems a bit like the concept of a static language compiler. Therefore, this feature has also been some people referred to as: JavaScript's "pre-compiled."

In most cases, we do not need to struggle these details. As long as you bear in mind: JavaScript is also a kind of code in the data, the same can be arbitrary assignment and modification, and its value is the code logic. However, with the general data are different, function calls can be implemented.

However, if the JavaScript function only Daoxing this point, it is with C + + the function pointer, DELPHI method pointer, C # compared to the commission, but also what he wonder why! However, JavaScript function of magic is also reflected in the other two aspects: First, the type of function function object itself has the capacity, and the other is the target object function function with the combination of transcendent ability.

Fantastic Object

Say for the first function of the capacity of the object.

Any one function can be dynamically added or for the removal of property, the property can be a simple type, can be objects, can also be other functions. In other words, the function has all the characteristics of the object, you can put to use when the object function. In fact, the function is the object, but the object of many than a pair of brackets "()" operator, the operator used to perform logic functions. Namely, the function itself can also be called, the general object can not be called, in addition to exactly the same. See the following code:

Js code

  1. function Sing ()
  2. (
  3. with (arguments.callee)
  4. alert (author + ":" + poem);
  5. );
  6. Sing.author = "Li Bai";
  7. Sing.poem = "Han Qin months, streaming video lighting Princess. Jade Guan Road on one, do not go to the End of the World";
  8. Sing ();
  9. Sing.author = "Lee war";
  10. Sing.poem = "sunrise Han days before moonset Yinshan. Pipa resentment daughter has been singing three thousand years";
  11. Sing ();

   function Sing()
    {
        with(arguments.callee)
          alert(author + ":" + poem);
    };
    Sing.author = "李白";
    Sing.poem = "汉家秦地月,流影照明妃。一上玉关道,天涯去不归";
    Sing();
    Sing.author = "李战";
    Sing.poem = "日出汉家天,月落阴山前。女儿琵琶怨,已唱三千年";
    Sing();

At this code, Sing function is defined, the given function dynamically Sing increased property author and poem. The author and the poem set to a different property of authors and poems, in the call Sing () when we can show different results. This example in a poetic way, let us understand the JavaScript function that is the essence of objects, but also feel the beauty of JavaScript language.

Well, the above described, we should count to understand the function and the types of things are the same object type of thing, this kind of thing was what we call "object." Indeed, we can go look at these "objects", because they both "property" also has "methods" do. However, the following code also will enable us to generate a new doubts:

Js code

  1. var anObject = (); / / an object
  2. anObject.aProperty = "Property of object"; / / object of a property
  3. anObject.aMethod = function () (alert ( "Method of object")); / / object as a means of
  4. / / Main look below:
  5. alert (anObject [ "aProperty"]); / / can be the target when the array to the property name as a subscript to access property
  6. anObject [ "aMethod "](); / / can be the target when the array name as a method to call methods subscript
  7. for (var s in anObject) / / traverse the object of all property and iterative methods of treatment
  8. alert (s + "is a" + typeof (anObject [s]));

  var anObject = {};  //一个对象
    anObject.aProperty = "Property of object";  //对象的一个属性
    anObject.aMethod = function(){alert("Method of object")}; //对象的一个方法
    //主要看下面:
    alert(anObject["aProperty"]);   //可以将对象当数组以属性名作为下标来访问属性
    anObject["aMethod"]();          //可以将对象当数组以方法名作为下标来调用方法
    for( var s in anObject)           //遍历对象的所有属性和方法进行迭代化处理
        alert(s + " is a " + typeof(anObject[s]));

For the same type of object function is the same:

Js code

  1. var aFunction = function () (); / / a function
  2. aFunction.aProperty = "Property of function"; / / function of a property
  3. aFunction.aMethod = function () (alert ( "Method of function")); / / function as a means of
  4. / / Main look below:
  5. alert (aFunction [ "aProperty"]); / / can be to function as an array subscript as the property name to access the property
  6. aFunction [ "aMethod "](); / / can function as an array to method name as a subscript to call the method
  7. for (var s in aFunction) / / traverse all property functions and methods of treatment iterative
  8. alert (s + "is a" + typeof (aFunction [s]));

var aFunction = function() {};  //一个函数
    aFunction.aProperty = "Property of function";  //函数的一个属性
    aFunction.aMethod = function(){alert("Method of function")}; //函数的一个方法
    //主要看下面:
    alert(aFunction["aProperty"]);   //可以将函数当数组以属性名作为下标来访问属性
    aFunction["aMethod"]();          //可以将函数当数组以方法名作为下标来调用方法
    for( var s in aFunction)           //遍历函数的所有属性和方法进行迭代化处理
        alert(s + " is a " + typeof(aFunction[s]));


Yes, objects and functions can be the same as the array, using the property name or method name as a subscript to access and deal with. Then, in the end it should be regarded as an array, or count objects?

We know that the array data structure should be regarded as linear, linear data structures in general must have the rules suitable to carry out a unified iterative batch operation, a bit like a wave. And targeted at discrete data structure, suitable to describe decentralized and personalized things, a bit like a particle. Therefore, we could ask: JavaScript's objects are waves or particles in the end?

If objects exist in quantum theory, the answer must be: wave-particle duality!

Therefore, JavaScript functions and objects in both the characteristics of the object also has an array of features. The array is called "dictionary", a name can be scalable set of values of children. In fact, object and function is an implementation of the internal dictionary structure, but the structure of this dictionary is an elaborate and sophisticated grammar show a rich appearance. As quantum mechanics particles used in some places to explain and deal with questions, while in other places they have made use of waves to explain and deal with questions. You can also required at the time, to free choice of the object or array to explain and deal with questions. Mastery of JavaScript as long as these wonderful features, you can produce a lot of simple and powerful code to.

Put object

Let us look at the transcendent function and object combination of now.

Object-oriented programming in the world, data and code constitutes the organic integration of the concept of object. Since the object, programming the world is divided into two parts, one is the object of the world, and one is outside the object world. Object innate selfish side, the outside world without a permit are not accessible within the object. Target also has generous side, which provide properties and methods, but also the service of others. However, here we want to talk about an interesting question, that is, "the object of self-awareness."

What? Heard of mistake, right? Object has self-consciousness?

Possible for many programmers, it is first heard. However, please take a look at C + +, C # and Java, this, DELPHI's self, there are VB of me, perhaps you will suddenly! Of course, it may only be "much better than" just.

However, at the object inside and outside the world is divided into two parts at the same time, the object "self" is the resulting. "Self-consciousness" are the most basic features of life! It is precisely because the object of such great vitality that makes programming world is full of infinite vigor and vitality.

But the object of "self-consciousness" Happiness at the same time brought to us has also brought pain and trouble. We give too much desire for the object given a total of hope that they can do more things. However, the object makes them selfish competition for system resources with each other, the object so that objects become self-complicated and bloated, self-deception is often the object brought about by lingering errors and exceptions. Why is there so much of our pain and trouble it?

To this end, there is a person, in the object tree, think of the whole九九八十一days, and finally realize his life suffering from the desire, but their desire to study the root causes are from the self-consciousness. So he put aside the "self" in the object tree has become a Buddha, then he began to Purdue creatures, dissemination真经. His name is called迦摩尼release, and "JavaScript真经" It is his Biography book by one.

JavaScript also has this, but that this is with C + +, C # or Java and other languages of this different. General programming language this is the object itself, and JavaScript are not necessarily of this! this may be me, or it may be you may be him, anyway, there you are me, you have me, which should not use the original "self" to understand the meaning of JavaScript that this has been. To this end, we must first put aside the original object that the "self."

We look at the following code:

Js code

  1. function WhoAmI () / / define a function whoami
  2. (
  3. alert ( "I'm" + this. name + "of" + typeof (this));
  4. );
  5. WhoAmI (); / / At this point it is this current period of global object code, in your browser is the window object, its name property to an empty string. Output: I'm of object
  6. var BillGates = (name: "Bill Gates");
  7. BillGates.WhoAmI = WhoAmI; / / will function as whoami BillGates method.
  8. BillGates.WhoAmI (); / / At this point of this is BillGates. Output: I'm Bill Gates of object
  9. var SteveJobs = (name: "Steve Jobs");
  10. SteveJobs.WhoAmI = WhoAmI; / / will function as whoami SteveJobs method.
  11. SteveJobs.WhoAmI (); / / At this point of this is SteveJobs. Output: I'm Steve Jobs of object
  12. WhoAmI.call (BillGates); / / directly to BillGates as this, call WhoAmI. Output: I'm Bill Gates of object
  13. WhoAmI.call (SteveJobs); / / directly to the SteveJobs as this, call WhoAmI. Output: I'm Steve Jobs of object
  14. BillGates.WhoAmI.call (SteveJobs); / / will SteveJobs as this, but the whoami call BillGates Ways. Output: I'm Steve Jobs of object
  15. SteveJobs.WhoAmI.call (BillGates); / / will BillGates as this, but the whoami call SteveJobs Ways. Output: I'm Bill Gates of object
  16. WhoAmI.WhoAmI = WhoAmI; / / whoami function will be set to its own methods.
  17. WhoAmI.name = "WhoAmI";
  18. WhoAmI.WhoAmI (); / / At this point of this function are whoami own. Output: I'm WhoAmI of function
  19. ((name: "nobody", WhoAmI: WhoAmI)). WhoAmI (); / / temporary to create an anonymous object and set the property after the method call whoami. Output: I'm nobody of object

   function WhoAmI()       //定义一个函数WhoAmI
    {
        alert("I'm " + this.name + " of " + typeof(this));
    };
    
    WhoAmI();   //此时是this当前这段代码的全局对象,在浏览器中就是window对象,其name属性为空字符串。输出:I'm of object

    var BillGates = {name: "Bill Gates"};
    BillGates.WhoAmI = WhoAmI;  //将函数WhoAmI作为BillGates的方法。
    BillGates.WhoAmI();         //此时的this是BillGates。输出:I'm Bill Gates of object
    
    var SteveJobs = {name: "Steve Jobs"};
    SteveJobs.WhoAmI = WhoAmI;  //将函数WhoAmI作为SteveJobs的方法。
    SteveJobs.WhoAmI();         //此时的this是SteveJobs。输出:I'm Steve Jobs of object

    WhoAmI.call(BillGates);     //直接将BillGates作为this,调用WhoAmI。输出:I'm Bill Gates of object
    WhoAmI.call(SteveJobs);     //直接将SteveJobs作为this,调用WhoAmI。输出:I'm Steve Jobs of object
    
    BillGates.WhoAmI.call(SteveJobs);   //将SteveJobs作为this,却调用BillGates的WhoAmI方法。输出:I'm Steve Jobs of object
    SteveJobs.WhoAmI.call(BillGates);   //将BillGates作为this,却调用SteveJobs的WhoAmI方法。输出:I'm Bill Gates of object

    WhoAmI.WhoAmI = WhoAmI;     //将WhoAmI函数设置为自身的方法。
    WhoAmI.name = "WhoAmI";
    WhoAmI.WhoAmI();            //此时的this是WhoAmI函数自己。输出:I'm WhoAmI of function
        
    ({name: "nobody", WhoAmI: WhoAmI}).WhoAmI();    //临时创建一个匿名对象并设置属性后调用WhoAmI方法。输出:I'm nobody of object


From the above code can be seen that the same function from different angles to call, this is not necessarily a function of the object of their own. this only in the arbitrary function objects and combine elements of a concept, is a combination of general objects than the default language combination more flexible, even more aloof and free and easy.

In the JavaScript function, you can only put this as a present to serve "this" object. this is a special built-in parameters, according to this parameter, you can visit to "this" object's properties and methods, but it should not give this parameter assignment. In general the target language, methods, precursors of this code can be omitted, the default is first and foremost members of their "own". However, JavaScript is different, because there is no "self", when the visit to "this" object, this can not be omitted!

JavaScript provides the transmission of this parameter in various forms and means, which, like BillGates.WhoAmI () and SteveJobs.WhoAmI () This form is to pass this parameter the most formal form, at this time this is the function object itself belongs . While the majority of cases, we rarely go borrow the use of those flower forms仙佛call. But we need to understand the JavaScript of the "self" and other programming languages the "self" is different, this is a put down of "self", which is unique worldview JavaScript.

Object sketch

Much has been said a lot of topics, but have a very basic question we forgot to discuss, and that is: How to set up the object?

In the previous example, we have involved the establishment of object. We use a called JavaScript Object Notation (abbreviation JSON) format, translated into Chinese is "JavaScript Object Notation."

JSON to create object to provide a very easy method. For example,
Create a property of any object:

Js code

  1. var o = ();

var o = {};

Create an object and set property and the initial value:

Js code

  1. var person = (name: "Angel", age: 18, married: false);

var person = {name: "Angel", age: 18, married: false};

Create an object and set properties and methods:

Js code

  1. var speaker = (text: "Hello World", say: function () (alert (this. text)));

var speaker = {text: "Hello World", say: function(){alert(this.text)}};

Create a more complex object, nested arrays and objects to other objects, such as:

Js code

  1. var company =
  2. (
  3. name: "Microsoft",
  4. product: "softwares",
  5. chairman: (name: "Bill Gates", age: 53, Married: true),
  6. employees: [(name: "Angel", age: 26, Married: false), (name: "Hanson", age: 32, Marred: true)],
  7. readme: function () (document.write (this. name + "product" + this. product);)
  8. );

  var company =
    {
        name: "Microsoft",
        product: "softwares",
        chairman: {name: "Bill Gates", age: 53, Married: true},
        employees: [{name: "Angel", age: 26, Married: false}, {name: "Hanson", age: 32, Marred: true}],
        readme: function() {document.write(this.name + " product " + this.product);}
    };

The form of JSON is to use large, including "()" including its list of items together, between each item and use a comma "," separated, while the project is to use a colon ":"-separated property name and property value. Dictionary This is a typical form of express, but also once again shows the JavaScript's object dictionary structure. No matter how complex objects can be a JSON code to create and assign.

In fact, JSON is JavaScript Object Serialization best form, it is also more concise than XML and more space. JSON objects can be used as a form of string networks at transmission and exchange information freely. And when necessary will be the JSON string into a JavaScript object, only need to use the eval function of this powerful digital conversion engine, on the immediate memory can be provided with a JavaScript object. It is precisely because of this simple JSON simple natural beauty, she makes the AJAX arena璀璨夺目become a star.

JavaScript is one such, who appears to put the complexity of object-oriented things, with its simple form of expression. To unload the object of heavy makeup vanity, but also the object of a clear prospect of a solution!