Home > Web Front-end > JS Tutorial > body text

Understanding JavaScript

高洛峰
Release: 2016-11-26 09:31:54
Original
882 people have browsed it

Introduction

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 inherently quiet and always wants to maintain its inherent nature; while code is inherently 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 worldviews, 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 the idea of ​​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: "I have finished teaching. 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 get dusty!". 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 Huineng sighed softly after looking at it, 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 scriptures that he had treasured for many years, and then asked him to escape overnight under the moonlight...

Later, Huineng really lived up to his master's high expectations. Created another vast sky for Zen Buddhism in the south. One of the software scriptures that Huineng took away was the "JavaScript Scripture"!

Back 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 concise. 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.

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, Function, etc. You know, the JavaScript language is case-sensitive!

Any JavaScript identifier, constant, variable and parameter is just one of unfined, null, bool, number, string, object and function types, which is what typeof returns The type indicated by the value. 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.
                                                                                                                                                                                petrelrelure, blown, tears, tears, she Shen- she Shen Shen Shen Shen Shen everywhere she she hopes she she races she her All she she she she she she needs she with E It can assign Undefined to any variable or attribute, but it does not mean that the variable is cleared, but it will have an additional attribute.

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. O 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.

                                                                                                                                                                                                                 spotsurustenururelrel - blown,ururel, she Shen Shen Shen Shen Shen Shen Shen Shen Shen Shen Shen Shen Shen her, her ordns to do herself to do her for.

The structure NaN participates in any numerical calculation is NaN, and NaN != 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.

There is 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:
var life = {};
for(life.age = 1; life.age <= 3; life.age++)
{
switch(life.age)
{
             case 1: life.body                                                                                                                                               ";
          life .gill = "gill";
life.body = "tadpole";
life.say = function(){alert(this.age+this.body+"-"+this.tail+","+this.gill)} ;
                                                                                             break;             life.lung = "Lung"; say = function(){alert(this.age+this.body+"-"+this.legs+","+this.lung)};
                               break; 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 of "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 understand complex reality The 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, at this time, you begin to feel something deep in your heart, 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 has only one form of 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:
function myfunc()
{
alert("hello");
};

alert(typeof(myfunc));

After running this code, you can see that typeof(myfunc) returns 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:
var myfunc = function ()
                                                                                                                                                                               ;
                                                                                                                                                                                                                                       alert(typeof(myfunc)); 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.

You may be smart and ask further questions immediately: Since functions are just variables, variables can be assigned values ​​​​arbitrarily and used anywhere???/p>

Let’s take a look at the following code:

var myfunc = function ( )

                                                                                          . "yeah");

};

myfunc(); //The second call to myfunc will output yeah


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:
function myfunc ()
{
alert("hello");
};
myfunc(); //Call here myfunc, output yeah instead of hello

function myfunc ()
{

alert("yeah");

};

myfunc(); //myfunc is called here, of course it outputs yeah


Logically speaking, the two signatures are exactly the same functions that 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. This is why?

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. That is to say, 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

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!