Programming world there are two basic elements of only one data, one is the code. Programming data and the world is entangled in a web of code displays unlimited vigor and vitality.
Data is inherently quiet, and always want to maintain their inherent qualities; and the code is natural and lively, always wanted to change the world.
You see, the relationship between source data and the relationship between material and energy have surprisingly similar. Data also inertia, if there is no code to exert external forces, she maintain its original state. As the energy source, he's the sole purpose of existence is to try to change the status of the original data. Change the data in the code at the same time, also because of the resistance data and in turn affect or change the trend of the original code. Even in some cases, data can be transformed into code, but the code may be transformed into data, and perhaps there is a similar form of E = MC2 equation does digital conversion. However, data and code is that the contradictions between this operation and unity, the total could reflect the laws of the computer world, which is exactly what we prepared the law of logic.
However, because different programmers have different world views, 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 and development of the world.
As we all know, the current thinking of the most popular programming is the idea of object-oriented programming. Why is the idea of object-oriented programming quickly swept the world? Because the idea of object-oriented data and code the first time into a continuum, and the concept of a simple object presented to the programmer. This mess all of a sudden those who will be the original algorithm and subroutines, as well as the tangled complexity of data structure, divided into the object of clear and orderly structure, and thus sort out the data and code in our hearts like the Mission乱麻end. We can have a clearer way of thinking, thinking in another more up to explore the vast world of programming.
End弘忍taught in the Legend "真经object" after the day he said to his people: "has just been finished, must Seoul sentiment should be, please write a偈子their own point of view." Great神秀disciples are all recognized as the most savvy senior,偈子he wrote: "who is the object tree, such as the type of heart as prescribed.朝朝ground dust, Do not can the dust alight!."此偈an immediate sensation caused师兄弟们, everyone says that great writing. Monk Huineng only look after the fire, sighed softly and easily on the wall wrote: "This object has no roots, the type is invisible. Have not a thing, where can the dust alight?." And then shook his head, left. We saw the偈子Huineng that: "What is written in a mess of, ah, can not read."弘忍master mechanic read the poem神秀praise also nodded his head, look at the poem Huineng mechanic shook his head in silence and after. On the night but弘忍called quietly to his own Buddhist temple Huineng will treasure for many years to teach in his真经software, and 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. And Huineng真经was taken out of the software is a "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, data and programming code, only the world's two basic elements, and these two elements also have a tangled relationship. JavaScript is to simplify the data and code to the most primitive level.
The data in JavaScript is very simple. Only simple data undefined, null, boolean, number and string five, and complex data, only one, that is, object. This is like Chinese classical simple materialist ideology, the most basic elements of the world classified as金木水火土, and other complex substances are composed of five basic elements.
JavaScript code in the form only as a manifestation of that function.
Note: The above words are lowercase, do not and Number, String, Object, Function built-in functions, such as JavaScript confused. Need to know, JavaScript language is case-sensitive you!
JavaScript of any logo, constants, variables and parameters are unfined, null, bool, number, string, object and function types, typeof also showed that the type of return value. In addition there are no other types of.
First to give you a simple data types.
undefined: unknown on behalf of all things, nothing, can not imagine it will not be able to code as well.
Note: typeof (undefined) return is undefined.
Undefined can be assigned to any variable or attribute, but this does not mean that the removal of the variables, it will be more than one attribute.
null: there is then a concept, but nothing. None of it seems, there are also free. Although hard to imagine, but can be used to deal with the code.
Note: typeof (null) return object, but null is not the object, the variables with null values does not object.
boolean: is that the non-non, there is no doubt. On the right, it would be wrong, absolutely clear. Can be code to deal with, but also can control the code flow.
number: linear things, size, and the order is clear, rather than more chaos. Code to facilitate batch processing and control code and the iterative cycle.
Note: typeof (NaN) and typeof (Infinity) return 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, the code which the intention of understanding, and so on has been relied.
Simple types are not objects, JavaScript objects do not confer the ability of these simple types. Have been given direct simple identifier constant value types, 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 of providing the ability to object.
No category
object is the type of object. No matter how in the JavaScript in complex data and code can be organized into the form of the target object.
However, JavaScript has no "type" concept!
For many object-oriented programmers, this is probably the most difficult to understand JavaScript place. Yes ah, talk about almost any object-oriented book, 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 pillar六神无主am. It seems that objects and classes to put aside to achieve "the target of the non-root, type is invisible," the state is indeed not be easy ah.
In this way, we first look at a section of JavaScript procedure:
var life = ();
for (life.age = 1; life.age <= 3; life.age + +)
(
switch (life.age)
(
case 1: life.body = "egg";
life.say = function () (alert (this.age + this.body));
break;
case 2: life.tail = "tail";
life.gill = "gills";
life.body = "tadpole";
life.say = function () (alert (this.age + this.body +"-"+ this.tail +","+ this.gill));
break;
case 3: delete life.tail;
delete life.gill;
life.legs = "four legs";
life.lung = "lung";
life.body = "frog";
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 when the object is only a naked, without any attributes and methods. The first time in the life course, there is a physical property of body, and a way to say, appears to be an "egg." In the course of the second life, it grows the "tail" and "gills", with the tail and gill attribute, it is clear that it is a "tadpole." In the course of the third life, and its tail and gill attributes disappear, but grow the "four legs" and "lung", with the legs and lung properties, which eventually turned into a "frog." If you, your imagination, then perhaps it can become a handsome "prince", married to a beautiful "princess," what. However, after watching this program, please ponder one question:
We will need to type it?
Still remember the childhood that "a small tadpole to find her mother," the fairy tale it? Maybe yesterday evening, your child happens to be in this beautiful fairy to sleep in the bar. Lovely little tadpole that is, type in their own evolving process, gradually turned into the same mother, "category" in order to find his mother. The fairy tale that contains the programming philosophy is this: the object "type" is developed from scratch, evolving and, eventually disappear into the ...
"Class", it can help us understand the complexity of the real world, this confusion of the real world is indeed to be classified. However, if our thoughts were, "category" locking, and "Class" will become "tired." Imagine, if a life that began when the object has been provided on a fixed "category", it also the evolution of it? You can become a frog tadpoles? Can also speak to the children's mother to find a small tadpole story?
So, JavaScript is not "category", category has been based on the intangible, together with the object. It was put down "type" concept, JavaScript is the object of 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.
Magic function
Next, we discuss the magic of your JavaScript functions.
JavaScript code can only function as a form, function is the type of function. Perhaps there are other programming language code, such as procedure or method, but only function in JavaScript form. When we write a function, the only function is to establish a type of entity it. See the following procedures:
function myfunc ()
(
alert ( "hello");
);
alert (typeof (myfunc));
After running this code can be seen typeof (myfunc) to return to the function. Written more than a function of what we call the "definition" type of, if we are to read the following "variable type", and even easier to understand:
var myfunc = function ()
(
alert ( "hello");
);
alert (typeof (myfunc));
Here a clear definition of a variable myfunc, its initial value is given a function of the entity. Therefore, typeof (myfunc) is to return to the function. In fact, the wording of these two functions are equivalent, apart from a little nuances, and its internal realization of the same. In other words, we write the JavaScript function of these is a variable life more than the name, the variable type is the function, the variable's value is what we prepared a function of the code body.
Clever immediately you may be asked further: Since the function is variable, then the variables can be assigned arbitrarily and used anywhere Hello?
We take a look at the following code:
var myfunc = function ()
(
alert ( "hello");
);
myfunc (); / / first call myfunc, output hello
myfunc = function ()
(
alert ( "yeah");
);
myfunc (); / / second call myfunc, the output yeah
The results of running this process tells us: The answer is yes! Function in the first call, the function of the variable was given a new code of the function body, making the second call of the function, a different output.
Well, we came to the code above into the first definition of function forms:
function myfunc ()
(
alert ( "hello");
);
myfunc (); / / here call myfunc, output hello instead of yeah
function myfunc ()
(
alert ( "yeah");
);
myfunc (); / / here call myfunc, of course, yeah output
Reason, the two signatures exactly the same function, in other programming languages should be illegal. However, in JavaScript, this is true. However, after the program has found a strange phenomenon: twice to call that function only in the final output value! The first function is obviously not play any role. This is Why?
The original, JavaScript implementation of the engine is not a line by line analysis and implementation of procedures, but for some analysis of the implementation period of the. Moreover, in the same paragraph, the analysis of the implementation process, the definition of function words will be extracted to give priority to implementing. After the implementation of function definition, before any other statement the order code. In other words, before the first call myfunc, the first definition of the code function statement logic, has been the definition of the second statement covering function. Therefore, both the implementation of the final call is a function of the logic.
If the JavaScript code is divided into two sections, for example, they write in a html, and with its <script/> label is divided into two such:
<script>
function myfunc ()
(
alert ( "hello");
);
myfunc (); / / here call myfunc, output hello
</ script>
<script>
function myfunc ()
(
alert ( "yeah");
);
myfunc (); / / here call myfunc, output 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 is also known as some people: 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 logic of code. However, with the general difference is that data, function calls can be implemented.
However, if the JavaScript function Daoxing this point only, it is with C + + the function pointer, DELPHI method pointer, C # compared to the commission and what he wonder why! However, JavaScript function of magic is also reflected in the other two aspects: First, the type of function itself also has a function of the capacity of the object, and the other is the target object function function with the combination of transcendent ability.
Wonderful objects
First is that the function of the capacity of the object.
Can be a function of any of its dynamically add or remove attributes, these attributes can be simple types, which can be objects, as well as 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 more than a pair of brackets "()" operator, the operator used to implement 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:
function Sing ()
(
with (arguments.callee)
alert (author + ":" + poem);
);
Sing.author = "Li Bai";
Sing.poem = "Han Qin, the streaming video lighting Princess. One on Yu Guan Road, do not go to the End of the World";
Sing ();
Sing.author = "Lee war";
Sing.poem = "sunrise Han days before moonset Yinshan. Pipa resentment daughter has been singing three thousand years";
Sing ();
In this code, Sing function is defined, the given function dynamically Sing increased property author and poem. Author and the poem will be set to a different attribute the author and poem, in the call Sing () when we can show different results. The sample in a poetic way, let us understand the JavaScript function that is the essence of objects, but also felt the beauty of JavaScript language.
Well, the above described, we should count to understand what type of function and object types are the same thing, this kind of thing was what we call "object." Indeed, we can look at these "objects" because they both "property" there are "methods" do. However, the following code and let us create new doubts:
var anObject = (); / / an object anObject.aProperty = "Property of object"; / / object of an attribute anObject.aMethod = function () (alert ( "Method of object")); / / object as a means of / / Main to see the following:
alert (anObject [ "aProperty"]); / / can be the target when the array to the property name as a subscript to access the properties anObject [ "aMethod "](); / / can be the target when the array name as a method to call the subscript method for (var s in anObject) / / traverse all the attributes of objects and methods to deal with iteration of alert (s + "is a" + typeof (anObject [s]));
For the same type of object function is the same:
var aFunction = function () (); / / a function aFunction.aProperty = "Property of function"; / / function of an attribute aFunction.aMethod = function () (alert ( "Method of function")); / / function One way of / / the main to see the following:
alert (aFunction [ "aProperty"]); / / can function as an array to the property name as a subscript to access the properties aFunction [ "aMethod "](); / / can function as a method name as the array subscript to call method for (var s in aFunction) / / traverse function for all properties and methods to deal with iteration of 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 process. 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 have a certain regularity, suitable for a unified iterative batch operation, a bit like a wave. Is discrete and data structure, suitable to describe the scattered and individual things, a bit like a particle. Therefore, we can also ask: JavaScript's object in the end is a wave or particle?
Object if there is 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 to children. In fact, object and function of the internal structure of the realization of a dictionary, but the structure of this dictionary is an elaborate and sophisticated syntax to show a rich appearance. Quantum mechanics, as used in some places particles to explain and deal with the problem, while in other places with waves to explain and deal with the problem. You can also when necessary, to free choice of the object or array to explain and deal with the problem. Mastery of JavaScript as long as these wonderful features, you can prepare a lot of simple and powerful code.
Put object
Let us look at the transcendent function combined with the object bar.
Object-oriented programming in the world, data and code constitutes the organic integration of the concept of the object. Since the object has, programming the world has been divided into two parts, one is the object of the world, a world outside the object. Natural objects with the selfish side, the outside world can not visit without permission within the target. Target also has generous side, which provide properties and methods, as well as other services. However, here we want to talk about an interesting question, that is, "the object of self-awareness."
What? Not picked it up wrong, right? Are there self-consciousness?
Possible for many programmers, this is the first time I heard. However, please take a look at C + +, C # and Java, this, DELPHI's self, there are VB's me, perhaps you will suddenly! Of course, only be "much better than" just.
However, the object will be in the world is divided into two parts, both inside and outside at the same time, the object "self" is the resulting. "Self-awareness" is the most basic features of life! It is precisely because the object of such great vitality that makes programming the world is full of infinite vigor and vitality.
But the object of "self-consciousness" brought us in at the same time happy and worries brought about by the pain. We object to the desire to give up too much, hope they can do more things. However, the objects 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 do we have so much pain and trouble it?
To this end, there is a person, in the object tree, exactly like the九九八十一days, and finally realize the pain of life comes from the desire, but the root is the root cause of desire from the self-consciousness. So he put the "self" in the object tree has become a Buddha, he began to Purdue from the people, the spread of真经. His name is called迦摩尼release, and "JavaScript真经" by Chuan-of his book by one.
JavaScript also has this, but this is with C + +, C # or Java and other languages of this difference. General programming language of this is the object itself, and JavaScript for this but not necessarily! this may be me, it may be you may be him, anyway, I have you, you have me, which can 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 target of the "self."
We look at the following code:
function WhoAmI () / / definition of a function WhoAmI
(
alert ( "I'm" + this.name + "of" + typeof (this));
);
WhoAmI (); / / At this point it is this current period of global object code, in your browser is the window object, its name attribute to an empty string. Output: I'm of object
var BillGates = (name: "Bill Gates");
BillGates.WhoAmI = WhoAmI; / / will function as a WhoAmI way BillGates.
BillGates.WhoAmI (); / / At this point this is the BillGates. Output: I'm Bill Gates of object
var SteveJobs = (name: "Steve Jobs");
SteveJobs.WhoAmI = WhoAmI; / / will function as a WhoAmI way SteveJobs.
SteveJobs.WhoAmI (); / / At this point this is the SteveJobs. Output: I'm Steve Jobs of object
WhoAmI.call (BillGates); / / directly to BillGates as this, call WhoAmI. Output: I'm Bill Gates of object
WhoAmI.call (SteveJobs); / / directly to the SteveJobs as this, call WhoAmI. Output: I'm Steve Jobs of object
BillGates.WhoAmI.call (SteveJobs); / / will SteveJobs as this, but the WhoAmI method calls BillGates. Output: I'm Steve Jobs of object
SteveJobs.WhoAmI.call (BillGates); / / will BillGates as this, but the WhoAmI method calls SteveJobs. Output: I'm Bill Gates of object
WhoAmI.WhoAmI = WhoAmI; / / will be set up for their own WhoAmI function method.
WhoAmI.name = "WhoAmI";
WhoAmI.WhoAmI (); / / At this point this is the function of their WhoAmI. Output: I'm WhoAmI of function
((name: "nobody", WhoAmI: WhoAmI)). WhoAmI (); / / temporary to create an anonymous object and set attributes WhoAmI method called after. Output: I'm nobody of object
Code from the above we can see that the same function can be called from different points of view, this is not necessarily a function of the object of their own. this is only in the arbitrary function objects and combine elements of a concept, is a combination of the target language than the default in general combined with more flexible, even more independent and free and easy.
In the JavaScript function, you can only use this as a present to serve "this" object. this is a special built-in parameters, according to this parameter, you can visit the "this" object's properties and methods, but it can not assign to this parameter. Target language in general, the method body 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 () in this way, the passing of this parameter the most formal form of this is to function at this time the object of their own . And in most cases, we have to adopt the little flowers to form仙佛call. However, we need to understand the JavaScript only the "self" and other programming languages the "self" is different, this is a put down of the "self", which is unique to the world JavaScript.
Drawing Object
Much has been said a number of topics, but there is a very basic question we forgot to discuss, that is: How to set up the object?
In the previous example, we have involved the establishment of the object. We use a called JavaScript Object Notation (abbreviation JSON) format, translated into English is "JavaScript Object Notation."
JSON object to create a very simple method. For example,
Create an object without any attribute:
var o = ();
To create an object and set attributes and initial value:
var person = (name: "Angel", age: 18, married: false);
To create an object and set properties and methods:
var speaker = (text: "Hello World", say: function () (alert (this.text)));
Create a more complex object, nested arrays and objects to other objects, such as:
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);)
);
JSON is the form of large, including "()" is a list of items, including up, each with a comma between "," separated, and the project is to use a colon ":" separated from the attribute name and attribute value. This is a typical representation of the dictionary, but also once again shows that the JavaScript is in the dictionary structure of the object. No matter how complex objects can be to create a JSON code and assignment.
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, free delivery in the network and exchange information. And when necessary to the JSON string into a JavaScript object, only need to use the eval function of the digital conversion of the powerful engine, as soon as the memory can be a JavaScript object. It is precisely because of this simple JSON simple natural beauty, she makes the AJAX arena璀璨夺目become a star.
JavaScript is the case, the object-oriented things that may seem complex, with its simple form of expression. Vanity object to unload the heavy makeup, but also the object of a clear prospect of a solution!
Structure object
Well, take us to discuss the creation of the object to another method.
In addition to JSON, in JavaScript, we can use the new operator of the form combined with a function to create objects. For example:
function MyFunc () (); / / define an empty function var anObj = new MyFunc (); / / use the new operator, with MyFun function, on the creation of an object
JavaScript to create objects of this can be really interesting to understand how such a manner?
In fact, the above code can be rewritten into the form of this equivalence:
function MyFunc (){};
var anObj = (); / / create an object MyFunc.call (anObj); / / will anObj as this pointer to call the object function MyFunc
We can interpret it this way, JavaScript first new operator to create an object, this object will be followed by this parameter as a function of the back call. In fact, JavaScript is done within, and any function can be called such! However, "anObj = new MyFunc ()" in this way, we saw a familiar figure, C + + and C # do not create the object is it? It turned out that All roads lead to Lingshan, achieving the same ah!
Eagle would like to see here might be, Why can not we as a constructor of the MyFunc do? Congratulations, the correct answers! JavaScript is also a thought! See the following code:
1 function Person (name) / / constructor with parameters 2 (
3 this.name = name; / / the parameter values assigned to this object's properties 4 this.SayHello = function () (alert ( "Hello, I'm" + this.name);); / / to this object SayHello define a method.
5);
6
7 function Employee (name, salary) / / sub-constructor 8 (
9 Person.call (this, name); / / will this pass the parent constructor
10 this.salary = salary; / / set up a salary of this property
11 this.ShowMeTheMoney = function () (alert (this.name + "$" + this.salary);); / / add ShowMeTheMoney method.
12);
13
14 var BillGates = new Person ( "Bill Gates"); / / constructor used to create Person object BillGates
15 var SteveJobs = new Employee ( "Steve Jobs", 1234); / / constructor used to create Empolyee object SteveJobs
16
17 BillGates.SayHello (); / / show: I'm Bill Gates
18 SteveJobs.SayHello (); / / show: I'm Steve Jobs
19 SteveJobs.ShowMeTheMoney (); / / show: Steve Jobs $ 1234
20
21 alert (BillGates.constructor == Person); / / show: true
22 alert (SteveJobs.constructor == Employee); / / show: true
23
24 alert (BillGates.SayHello == SteveJobs.SayHello); / / show: false
This code shows that not only can function as a constructor, but also with parameters, can also add members and methods of the object. Article 9 of the line, Employee constructor will receive their call as a parameter of this constructor Person, which is equivalent to call the base class's constructor. 21,22 also show that the first line of such a meaning: BillGates constructed by the Person, and SteveJobs constructed by the Employee. Built-in constructor object attributes structure also identifies the specific object function used!
In fact, if you are willing to function as a "category", she is the "category", because she already has "class" of those characteristics. Is not it? Give birth to her son all have the same characteristics, but also with the type constructor of the same name it!
Yet it must be noted, the operation of this object constructor to create an object out of each, not only with members of their own data, but also with data in their own way. In other words, the method code body (embodiment of the logic function of the data) in every object there is a copy. Although a copy of each of the logic of the code are the same, but the object is to preserve a code of their body. The above example that the last sentence of the facts, which explains the function of JavaScript is that the concept of object.
The object of the same type of methods each of which has a code is obviously a waste. In a traditional programming language, the method does not function as a JavaScript object as the concept. Even if there are, as function pointer, methods







