Home >Web Front-end >JS Tutorial >Comprehensive version of JavaScript, page 1/2_javascript skills

Comprehensive version of JavaScript, page 1/2_javascript skills

PHP中文网
PHP中文网Original
2016-05-16 19:05:301010browse

There are only two basic elements in the programming world, one is data and the other is code. The world of programming shows infinite vitality and vitality in the inextricable entanglement of data and code.

Data is naturally quiet and always wants to maintain its inherent nature; while code is naturally lively and always wants to change the world.

You see, the relationship between data codes is surprisingly similar to the relationship between matter and energy. Data also has inertia. If there is no code to exert external force, it will always maintain its original state. Code is like energy. The only purpose of its existence is to work hard to change the original state of the data. When the code changes the data, it will also in turn affect or change the original trend of the code due to the resistance of the data. Even in some cases, data can be converted into code, and code may be converted into data. There may be a digital conversion equation similar to E=MC2. However, it is in this contradictory yet unified operation between data and code that the laws of the computer world can always be reflected. These laws are exactly the program logic we write.

However, because different programmers have different world views, these data and codes look different. As a result, programmers with different worldviews use their own methodologies to promote the evolution and development of the programming world.

As we all know, the most popular programming idea today is object-oriented programming. Why can object-oriented ideas quickly become popular in the programming world? Because object-oriented thinking combines data and code into a unity for the first time and presents it to programmers with a simple object concept. This suddenly divides the original messy algorithms and subroutines, as well as the entangled complex data structures, into clear and orderly object structures, thus clarifying the messy knot of data and code in our hearts. We can have a clearer mind and explore the vaster programming world from another level of thinking.

One day after the Fifth Patriarch Hongren finished teaching the "Object True Sutra", he said to all his disciples: "The lecture has been finished. I think you should have some insights. Please each write a verse to read. ". The eldest disciple, Shenxiu, is recognized as the senior brother with the highest understanding. His verse reads: "The body is like an object tree, and the mind is as bright as its kind. Wipe it diligently and don't let it stir up dust!". As soon as this verse came out, it immediately caused a sensation among the brothers. Everyone said it was so well written. Only the fire-headed monk Hui could look at it, sighed softly, and wrote on the wall: "The object has no roots, and the type is invisible. There is nothing in the first place, so where can it cause dust?" Then he shook his head and walked away. Everyone who read Hui Neng's gatha said, "It's written in such a mess that I can't understand it." Master Hongren read Shenxiu's verse and nodded in praise, and then looked at Huineng's verse and shook his head silently. That night, Hongren quietly called Huineng to his meditation room, taught him the software scripture that he had treasured for many years, and then asked him to escape overnight under the moonlight...

Later, Huineng really didn't Living up to Master's high expectations, he created another vast sky for Zen Buddhism in the south. One of the software scriptures that Huineng took away was the "JavaScript Scripture"!

Return to simplicity

To understand JavaScript, you must first let go of the concepts of objects and classes and return to the origins of data and code. As mentioned before, the programming world has only two basic elements: data and code, and these two elements are intertwined. JavaScript simplifies data and code to the most primitive level.

Data in JavaScript is very simple. There are only five types of simple data: undefined, null, boolean, number and string, while there is only one type of complex data, namely object. This is like classical Chinese simple materialism, which classifies the most basic elements of the world as metal, wood, water, fire, and earth, and other complex substances are composed of these five basic elements.

The code in JavaScript is only reflected in one form, which is function.

Note: The above words are all lowercase, do not confuse them with JavaScript built-in functions such as Number, String, Object, and Function. You know, the JavaScript language is case-sensitive!

Any JavaScript identifier, constant, variable and parameter is just one of the unfined, null, bool, number, string, object and function types. That is, the type indicated by the return value of typeof. There is no other type besides this.

Let’s talk about simple data types first.

undefined: represents all unknown things, nothing, unimaginable, and the code cannot handle it.
Note: Typeof (undefined) return is also undefined.
You can assign undefined to any variable or property, but it does not mean that the variable is cleared, but it will add an additional property.

null: There is a concept, but nothing. There seems to be something within nothing, and nothing within something. Although it is difficult to imagine, it can already be handled with code.
Note: Typeof (Null) returns Object, but NULL is not Object, and variables with NULL values ​​are not Object.

boolean: Yes is, no is no, there is no doubt. Right is right, wrong is wrong, absolutely clear. It can be processed by the code and can also control the flow of the code.

Number: Linear things, clear in size and order, numerous but not chaotic. It facilitates batch processing of code, and also controls the iteration and looping of code.
Note: Typeof (nan) and Typeof (Infinity) both return Number.
The structure of NaN participating in any numerical calculation is NaN, and NaN != NaN.
Infinity / Infinity = NaN.

string: Rational things for humans, not machine signals. Human-computer information communication, code understanding of human intentions, etc. all rely on it.

Simple types are not objects, and JavaScript does not give these simple types the ability to objectify. Identifiers, variables, and parameters that are directly assigned constant values ​​of simple types are not objects.

The so-called "objectification" is the ability to organize data and code into complex structures. In JavaScript, only the object type and function type provide objectification capabilities.

No class

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

But JavaScript does not have the concept of "class"!

For many object-oriented programmers, this is probably the most difficult thing to understand in JavaScript. Yes, in almost any object-oriented book, the first thing to talk about is the concept of "class", which is the pillar of object-oriented. Suddenly there is no "category", and we feel like we have lost our spiritual support and feel that we have no master. It seems that it is not easy to let go of objects and classes and reach the state where "objects have no roots and types are invisible".

So, let’s look at a JavaScript program first:


[Ctrl A to select all Note: If you need to introduce external Js, you need to refresh to execute]


This JavaScript program initially generated a life object, life. When life was born, it was just a bare object without any properties or methods. In the first life process, it has a body attribute and a say method, which looks like an "egg cell". During its second life, it grew a "tail" and "gills". With the tail and gill attributes, it was obviously a "tadpole". During its third life, its tail and gill attributes disappeared, but it grew "four legs" and "lungs", acquired the legs and lung attributes, and finally became a "frog". If you have a rich imagination, you may be able to turn it into a handsome "prince" and marry a beautiful "princess" or something. However, after reading this program, please think about a question:

Do we definitely need classes?

Do you still remember the fairy tale about "a little tadpole looking for its mother" when you were a child? Maybe just last night, your child happened to fall asleep in this beautiful fairy tale. The cute little tadpole gradually became the same "kind" as its mother during the continuous evolution of its own type, and thus found its mother. The programming philosophy contained in this fairy tale is: the "class" of an object comes from scratch, continues to evolve, and eventually disappears...

"Class" can indeed help us To understand the complex real world, this chaotic real world does need to be classified. But if our thoughts are bound by "category", "category" will become "tired". Imagine, if a living object is assigned a fixed "class" from the beginning, can it still evolve? Can a tadpole turn into a frog? Can you also tell the children the story of a tadpole looking for its mother?

Therefore, there is no "class" in JavaScript. Classes have become invisible and integrated with objects. It is precisely because of letting go of the concept of "class" that JavaScript objects have vitality that other programming languages ​​do not have.

If you begin to feel something deep in your heart at this time, then you have gradually begun to understand the Zen of JavaScript.

The magic of functions

Next, let’s discuss the magic of JavaScript functions.

JavaScript code only has one form: function, and function is the type of function. Maybe other programming languages ​​have code concepts such as procedure or method, but in JavaScript there is only one form of function. When we write a function, we just create an entity of type function. Please look at the following program:


[Ctrl A to select all Note: If you need to introduce external Js, you need to refresh before executing]


You can see this code after running it What typeof(myfunc) returns is function. We call the above function writing method "definition formula". If we rewrite it into the following "variable formula", it will be easier to understand:


[Ctrl A Full Optional note: If you need to introduce external Js, you need to refresh it before executing]


A variable myfunc is clearly defined here, and its initial value is assigned to a function entity. Therefore, typeof(myfunc) also returns function. In fact, the writing methods of these two functions are equivalent, and except for a few minor differences, their internal implementation is exactly the same. In other words, the JavaScript functions we write are just named variables. The variable type is function, and the value of the variable is the function code body we wrote.

If you are smart, you may immediately ask further: Since functions are just variables, variables can be assigned values ​​​​arbitrarily and used anywhere?

Let’s take a look at the following code:


[Ctrl A to select all Note: If you need to introduce external Js, you need to refresh to execute]


The result of running this program tells us: the answer is yes! After the function is called for the first time, the function variable is assigned a new function code body, so that when the function is called for the second time, different output appears.

Okay, let’s change the above code into the first defined function form:


[Ctrl A Select all Note: If you need to introduce external Js needs to be refreshed to execute]


It stands to reason that two functions with exactly the same signature should be illegal in other programming languages. But in JavaScript, this is true. However, after the program was run, a strange phenomenon was discovered: the two calls were only the values ​​output by the last function! Obviously the first function doesn't do anything. Why is this?

It turns out that the JavaScript execution engine does not analyze and execute the program line by line, but analyzes and executes it piece by piece. Moreover, during the analysis and execution of the same program, the defining function statements will be extracted and executed first. After the function definition is executed, other statement codes will be executed in sequence. In other words, before myfunc is called for the first time, the code logic defined by the first function statement has been overwritten by the second function definition statement. Therefore, both calls execute the last function logic.

If you divide this JavaScript code into two pieces, for example, write them in an html, and use the