A thorough understanding of JavaScript (an

2010-12-29  来源:本站原创  分类:Web  人气:116 

【】 Wutou turn JavaScript (a) 2010-11-30 11:20 Reprinted from the final edit goodfriend_001 from a beautiful people this is reproduced in the article, written very good, very good in understanding JavaScript, so Zhuantie over, find that multi-author Lee Battle (leadzen). Shenzhen 2008-2-23, did not find the original source.

Primer programming world there is only two basic elements, one is data, one is code. Programming world is inextricably entangled data and code to render unlimited vigor and vitality.

Data is inherently quiet, always want to maintain their inherent qualities; while the code is a natural and lively, always wanted to change the world.

You see, the relationship between the data code, the relationship between energy and matter are surprisingly similar. Data also have inertia, if not the code to apply force, she always kept their original state. The code is like energy, he exists for the sole purpose is to strive to change the data in original state. Change the data in the code at the same time, the resistance because the data will in turn affect or change the code to the original trend. Even in some cases, data can be transformed into code, but the code may be transformed into data, perhaps a similar form of E = MC2 does the digital conversion equation. However, that is, between the data and code that is contradictory and unified operation, the total could reflect the laws of the computer world, these laws is a program written in our logic.

However, due to different programmers have different world views, these data and code will look different. As a result, programmers have different world views their methodology used to promote the evolution and development of programming world.

As we all know, thinking of today's most popular programming than object-oriented programming ideas. Why is the idea of object-oriented programming quickly swept the world? Because the idea of object-oriented data and code the first time combine into unity, and the concept of a simple object presented to the programmer. This will be the original once the jumble of algorithms and subroutines, and the tangled complex data structures, divided into the object of clear and orderly structure, and thus sort out the data and code that mess in our hearts like a knot. We can have a more clear thinking, in another highly thought of programming up to explore a more vast world.

After teaching at the Fifth Patriarch Hongren "object Scriptures" the day after, he said unto his disciples: "has been finished, there should be some sentiment presumably Haier, please write their own point of view gatha." Disciple Shenxiu are all recognized as the most savvy senior, he gatha wrote: "the body is the object tree, such as class-like heart out. Zhao Zhao Qin wipe, not to let the dust alight!." Ciji an immediate sensation caused Shixiongdimen, everyone says that write nice. Only after attempting to see monk Hui Neng, gently sighed, and conveniently on the wall says: "object of the present without roots, the type is also invisible. Would have no one, where can the dust alight?." Then shook his head, walk away. We saw Hui Neng gatha said: "What is written in a mess, ah, can not read." Master Hongren Shenxiu read the verses are nods of approval, after a look at verses Huineng shook his head in silence. In the night, Hongren quietly to his Buddhist temple called Hui Neng, the software will be treasured for years to teach Scriptures to him and let him take advantage of the moonlight night away ...

Later, Master Hui Neng really live up to expectations, the other in the south created a Zen heaven. The software Huineng Scriptures in the year away, there is a "JavaScript Scriptures!"

Return to simple

To understand JavaScript, you must first put the concept of objects and classes, back to the primitive data and code. As mentioned above, the programming world, only two basic elements of data and code, and these two elements and has a tangled relationship. JavaScript code is to simplify the data and to the most primitive level.

JavaScript in the data is simple. Simple data only undefined, null, boolean, number, and string these five, only one, complex data, that object. This is like the simple material of Chinese classical thought, the most basic elements of the world classified as Five Elements, and other complex substances are composed by these five basic elements.

JavaScript code is only reflected in a form that function.

Note: These words are lowercase, do not and Number, String, Object, Function built-in functions such as JavaScript confused. You know, JavaScript language is case sensitive it!

The identity of any JavaScript, constants, variables and parameters are just unfined, null, bool, number, string, object and function types in one, it shows the type of return value of typeof. There is no other type of.

Let us talk about simple data types it.

undefined: on behalf of all the unknown, consequently, can not imagine, the code also will not be able to deal with.
Note: typeof (undefined) returns is undefined.
Undefined can be assigned to any variable or property, but does not mean the removal of the variable, it will therefore have one more attribute.

null: there is then a concept, but nothing. Nothing seems to be, there is also no. Although hard to imagine, but can already use the code to deal with.
Note: typeof (null) return object, but null is not the object, a null value of the variable is not object.

boolean: is is, non to Africa, no doubt. On on the right, wrong wrong, absolutely clear. Is code to handle both, you can control the code flow.

number: linear things, the size and well-sequenced, and more without being chaotic. Code to facilitate batch processing, and also control code iteration and circulation.
Note: typeof (NaN) and typeof (Infinity) both return number.
NaN participate in any numerical structure is NaN, and NaN! = NaN.
Infinity / Infinity = NaN.

string: the rational thing for human beings, not machines signal. Man-machine communication, understanding the intent of the code accordingly and so on, all depends on it.

Simple types are not objects, JavaScript objects have not given the ability of these simple types. Be given directly to a simple type identifier constant values, variables and parameters is not an object.

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

No class

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

However, JavaScript has no "class" concept!

For many object-oriented programmers, this is probably the most difficult to understand JavaScript in place. Yes ah, almost any book about object-oriented, the first talk about is the "class" concept, that was the pillar of object-oriented. This sudden lack of a "class", we do not like a spiritual pillar of a sudden felt Liushenwuzhu. It seems, to put down objects and classes, to "object of the present without roots, the type is also invisible," the state is indeed not an easy thing ah.

Thus, we first look at a JavaScript program:

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 ();
};

This JavaScript program started to have a life of a target life, life was born just a naked object, without any attributes and methods. In the first course of life, it had a physical property of body, and have a say method, appears to be an "egg." In the process of second life, it grows a "tail" and "gills", with the tail and gill property, obviously it is a "tadpole." In the third life course, its tail and gill properties disappear, but grow the "four legs" and "lung", with the legs and lung properties, which eventually turned into a "frog." If your imagination, then, might also make it a handsome "prince", married to a beautiful "princess" or something. However, after watching this program, please think about a problem:

We need to class?

Recite the "Little Tadpole Looking for Mom" in the fairy tale it? Maybe yesterday evening, your child just is in this beautiful fairy tale to sleep in it. Cute little tadpoles that is evolving in its own type of process, gradually became a mother and the same "class" in order to find his mother. This fairy tale contains a programming philosophy is: the object "class" from scratch, but also evolving and eventually disappear into the ...

"Class" can indeed help us understand the complex real world, this chaotic world, indeed the reality of the need for classification. But if our thinking is "Class" bound hand and foot, "Class" will become "tired." Imagine, if you start a life when the object is provided for fixed "class", it also evolved it? Tadpole into a frog can it? You can also told the children the story of tadpoles to mother it?

So, JavaScript is not "class", class is based on the intangible, and object integration. It is because down the "class" concept, JavaScript objects have no other programming language vitality.

If, when your heart began to perception, then you have come to begin to understand the Zen of JavaScript.

Magic function

Next, we discuss it about the magic of JavaScript functions.

JavaScript code is only a form of function, function is the function of the type. Perhaps there are other programming languages such as procedure or method code concept, but there is only function in a form of JavaScript. When we write a function of time, only the establishment of a function type of entity it. Consider the following program:

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

alert (typeof (myfunc));

After running this code you can see typeof (myfunc) returns the function. We call the function written above "definition" type, if we be rewritten as the following "variable type", it is easy to understand:

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

alert (typeof (myfunc));

This clearly defines a variable myfunc, its initial value is given a function of the entity. Therefore, typeof (myfunc) is returned function. In fact, the wording of these two functions are equivalent, except that nuance, its internal realization of the same. In other words, we write the JavaScript function is the name of a variable life only, the variable type is the function, the variable value is a function of the code we write the body.

Now you might be wise to further ask: Since the function is variable, the variable can be assigned arbitrarily and used anywhere then?

We take a look at the following code:

var myfunc = function ()
{
alert ("hello");
};
myfunc (); / / first call to myfunc, output hello

myfunc = function ()
{
alert ("yeah");
};
myfunc (); / / second call myfunc, yeah the output

The results of running this program tells us: The answer is yes! After the first call the function, the function variables has been given a new function code for the body, making the second call to the function, the emergence of different output.

Well, we came to the above code into the definition of the first type of functional form:

function myfunc ()
{
alert ("hello");
};
myfunc (); / / here call myfunc, the output instead of hello yeah

function myfunc ()
{
alert ("yeah");
};
myfunc (); / / here call myfunc, yeah of course, the output

It stands to reason, two signatures exactly the same function in other programming languages should be illegal. However, in JavaScript, you're right. However, the program runs, but after it discovered a strange phenomenon: two calls in just the last output value of that function! The first function clearly did not play any role. Why is this it?

Original, JavaScript execution engine does not line by line analysis and implementation process, but a period of analysis performed. Moreover, in the same analysis of the implementation of a program, define a function type statement is extracted from the precedence. Function definition executed only after the code in order to perform other statements. That is, before the first call to myfunc, the first definition of the code logic function statement, function definition has been the second statement covers. Therefore, the two are calling the last function is the implementation of the logic.

If the JavaScript code into two sections, for example, they write in a html, and use <script/> such labels will be divided into two:

<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 to each order, but also proved that JavaScript does perform paragraph by paragraph.

The definition section of the code will give priority to the implementation of type function statement, which seems a bit like the concept of a static language compiler. Therefore, this feature is also known some people: JavaScript's "pre-compiled."

In most cases, we do not need to dwell these details. As long as you remember that: JavaScript in the code is a kind of data can also be arbitrarily assigned and modified, and its value is the code logic. But, different from general data, the function can be called to implement.

However, if the JavaScript function only if the only point Daoxing, which is a function pointer C + +, DELPHI method pointer, C #, compared the commission, and what's surprising it! However, JavaScript function, the magic is also reflected in two aspects: First, the function itself has a function type object-oriented capabilities, and second, the function object transcendent function and object binding.

Unfinished, see the next one: a thorough understanding of JavaScript (II)

Category: Js Css | | Add to collection | share i posted it | View (9) | Comments (0) a: ssh concrete steps to integrate the next one: a thorough understanding of JavaScript (b) Related articles: • a thorough understanding of JavaScript (June) • a thorough understanding of JavaScript (V)
• a thorough understanding of JavaScript • a thorough understanding of JavaScript - highly recommend the jav ...
• a thorough understanding of JavaScript (d) • a thorough understanding of JavaScript (c)
• a thorough understanding of JavaScript (5) wonderful objects • a thorough understanding of JavaScript (4) a thorough understanding of the function of magic • JavaScript (2)

相关文章
  • Understanding of JavaScript closures 2009-03-11

    Understanding of JavaScript closures To become advanced JavaScript programmers, it is necessary to understand the closure. In this paper, ECMA 262 specification explain the closure of the internal working mechanism for JavaScript programmers understa

  • [Reprinted] in-depth understanding of JavaScript closure (closure) 2010-08-20

    Reprinted] [ depth understanding of JavaScript closure (closure) [reproduced Source: http://www.felixwoo.com/archives/247] Recent on-line access to a lot of Javascript closure (closure) related information is written mostly academic and professional.

  • A thorough understanding of JavaScript 2010-10-09

    A thorough understanding of JavaScript Programming world, there is only two basic elements, one for data, one is code. Programming world is inextricably entangled data and code to render unlimited vigor and vitality. Data is inherently quiet, always

  • A thorough understanding of JavaScript (an 2010-12-29

    [] Wutou turn JavaScript (a) 2010-11-30 11:20 Reprinted from the final edit goodfriend_001 from a beautiful people this is reproduced in the article, written very good, very good in understanding JavaScript, so Zhuantie over, find that multi-author L

  • Depth understanding of Javascript closure (closure) 2009-03-06

    Recently, online access to a lot of Javascript closure (closure) of relevant information and writing are very academic and professional. For beginners to understand let alone closure, and even the language describing it is very difficult to understan

  • Further understanding of javascript objects. Array and hash table [reproduced] 2010-04-05

    In javascript, the object is actually a hash table, such as following the user object: function user (n, a) ( this.name = n; this.age = a; this.toString = function () ( return "Name:" + this.name + ", Age:" + this.age; ) ) var u = new

  • In-depth understanding of Javascript closures (Reprinted) 2010-04-18

    Recent on-line access to a lot of Javascript closure (closure) related information is written mostly academic and professional. For beginners, let alone understand the closure, and even the narrative are hard to understand. The purpose of this writin

  • (Turn) in-depth understanding of Javascript closure (closure) 2010-04-24

    Recent on-line access to a lot of Javascript closure (closure) related information is written mostly academic and professional. For beginners, let alone understand the closure, and even the narrative are hard to understand. The purpose of this writin

  • In-depth understanding of Javascript closures 2010-05-29

    Recent on-line access to a lot of Javascript closure (closure) related information is written mostly academic and professional. For beginners, let alone understand the closure, and even the narrative are hard to understand. The purpose of this writin

  • In-depth understanding of Javascript closure (closure) 2010-05-31

    Thank http://softbbs.pconline.com.cn/9497825.html Recent on-line access to a lot of Javascript closure (closure) related information is written mostly academic and professional. For beginners, let alone understand the closure, and even the narrative

  • Transfer: in-depth understanding of javascript closures 2010-06-01

    Closure of the microscopic world If you want a more in-depth understanding of the closure and the functions a and b of the relationship between the nested function, we need to introduce several other concepts: the function of the execution environmen

  • Understanding of JavaScript in the sandbox 2010-06-08

    javascript in the sand box is not in the traditional sense of the sand box, only a grammatical hack written it, javascript processing module dependencies in the closure is called sandbox, and ajax, as this is a sandbox coding style kinds of phenomena

  • In-depth understanding of Javascript closures (closure) 2010-12-13

    I. What is closure? "Official" explanation: the so-called "closure" refers to a binding with many variables and the environment of these variables the expression (usually a function), and thus these variables are part of the expression

  • From shallow to deep understanding of JavaScript category 2009-10-28

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>&g

  • Understanding of Javascript array (ZZ) 2010-03-31

    From http://www.blueidea.com/tech/web/2009/6958.asp 1. Understand the array Array is a collection of certain data, the data type can be integer, string, or even an object Javascript does not support multidimensional arrays, but because of the array which

  • Some understanding of JavaScript keywords 2010-04-08

    this keyword (1) this is the object for variable or function (2) this on behalf of the object, if this in the constructor, this representative of the object being constructed, or call this function on behalf of the object Note: This is the most poorl

  • In-depth understanding of javascript eval function (Reprinted) 2010-05-07

    Found to be an appropriate title since this is not so easy, huh, huh, so in this note under the first two purposes of this article: (1) Introduction javascript eval function in the use of (2) how to function in the implementation of the global code ►

  • JavaScript can multi-threaded? In-depth understanding of JavaScript regular mechanism 2010-05-15

    Transfer from: http://www.phpv.net/html/1700.html The setTimeout and setInterval JavaScript are two very easy ways to deceive the feelings of others, because we often think that call will begin according to the established manner, I think many people

  • Tell you what is elegant code (11 )---- a thorough understanding of javascript 2010-11-30

    With the insertion of HTML5 RIA G-point touch, b / s will further liberate the productive forces, will further increase production efficiency, which will lead to changes in production relations, namely, from the c / s structure of the conservatism, l

  • In-depth understanding of Javascript 2011-01-05

    JavaScript Atomic (Atom) is QoBean an important concept raised, borrowed from the erlang, but the latter has a different meaning. In QoBean in, Meta (yuan) and Atom (atoms) are a pair of concepts, the former showed the smallest unit of the implementa