No-nonsense JavaScript tutorial (Full Set) _ javascript skills

Source: Internet
Author: User
Tags command access
Many readers think that I have been reading the essence of JavaScript language and programming practices, so I have always wanted to write a simple reader. Simply put, this is the simplest one. Many readers think that I have been reading the essence of JavaScript language and programming practices, so I have always wanted to write a simple reader. Simply put, this is the simplest one.

There is an article named "no nonsense ErLang" in the 2008.09 issue of programmer, which reminds me of many articles such as "no nonsense C" and "no nonsense book reviews, I also thought that JavaScript does not have a "no nonsense", so I decided to write this article. This decision is related to another reason: many readers think that I have been reading the essence of JavaScript language and programming practices, so I have always wanted to write a simple reader. Simply put, this is the simplest one.

Declaration: If you only want to read complicated things, do not read this article.
I. JavaScript was originally a procedural
JavaScript, which dates back to the 1.0 age, is actually a process. There are only two basic features. One is the HTML
Tag to take over the event, for example:

The Code is as follows:




The second option is to support a simple object constructor (function ). In fact, the constructor in this era is better than the initialization function. It should
Write as follows:

The Code is as follows:


Function MyObject (){
This. xxx = '...';
This. yyy = '...';
}
Obj = new MyObject ();


Therefore, early JavaScript was undoubtedly carrying the name "Object-based procedural scripting language. Besides
For the above two features, JavaScript has the nature of some general scripting languages, such:
-The entire. js file is loaded to the execution environment (for example, a WEB browser) at a time. After a syntax analysis, the file is executed row by row;
-During the preceding syntax analysis period, (named) Functions and variables declared with "var" are pre-processed in an identifier table for use by script code;
-From the execution of global code lines or function calls, the code that cannot be executed in the whole process is not found in the error (except for the syntax check in step 1 ).
It also has the nature of common procedural languages, such:
-Statements such as if/for/while/switch are available;
-Declare a function with function, and use "(...)" to declare its form parameter table, as well as to indicate function call and parameter passing;
-Similar to the basic syntax of C language, including the use of "{...}" to represent the code block, and the use "! = "And other operators;
-A basic concept similar to the object operation operator "." in Java, as well as attributes and methods.
Now you see a basic JavaScript language. Its Code only has functions and statement lines like C, and supports a very simple area.
Program to an object. Okay, this is almost all about JavaScript ...... Hmm ...... All basic syntax concepts. If you use one
If you get started with a little bit of programming language, you will think JavaScript is actually quite simple.
Yes, "Write a function and call it" is that simple. For example:
Function hi (){
Alert ('hello, world .');
}
Hi ();
2. The data type is slightly more complex.
JavaScript has six basic data types. One type is the value type, that is, undefined, string, number, and boolean.
Is the reference type, that is, function and object. To check the type of data X, you can simply use "typeof X" to return a string.
In other advanced languages, the value type differs from the reference type by using "pass value or reference during access. Simply put, in the following function
Medium:
Function foo (X ){
}
X indicates the value itself or a reference pointing to the value (you can think of it as a pointer), indicating the type of X. Different from other languages
Yes. JavaScript does not add a indicator to the call entry to demonstrate the method of explicitly passing values. For example:
Function foo (var X ){
// In general advanced languages, var indicates that the reference of variable X is always input.
}
Instead, the script engine determines how to pass the Value Based on the Data Type of X actually passed in. For example:
Function foo (X ){
...
}
Foo ('20140901'); // <-string '20160901' Value
Foo (aObj); // <-aObj is an object reference
The key to such processing is that the JavaScript type system is concise enough. The six basic types include three philosophical concepts: executable
And non-executable; object or non-object; has (value) or no (value ). Obviously, it is not easy to understand this philosophical idea, because it is more complex.
The self-contained logic is: The function is also an object, the value is also an object, and the non-value is also a value.
This is all about JavaScript systems. If you want to use it easily, remember the following:
-Three simple value types: string, number, and boolean are used for display on the webpage;
-Objects are used to store other objects, funtion, or the preceding simple value types, and are found using an attribute name using the '.' operation;
-Undefined is used to check whether the data is valid or invalid;
-Function is used for execution.
Of course, if you want to become a thinker or linguistic madman, think about the philosophical proposition above and I will not block you.
3. What I can use my nose to figure out is the amount of data.
Many people may not understand the Direct Volume declaration in JavaScript, but it is indeed very simple. Since most of our advanced languages are
Constant declaration is supported, and even the original assembly language supports immediate values. For example:
// In C
# Define ABYTE 256
// In delphi
Const
ABYTE = 256
; In asm
Mov ah, 256

Of course, JavaScript ...... Yes ...... They can support direct traffic without any clear background-they are actually a concept. For example:
// In javascript
Var
AByte = 256
When you understand it, remember: In all the above Code, the so-called direct or immediate value refers to the '123', not
Variable or constant identifier ABYTE/aByte. Further, You Need To Know That JavaScript supports eight types of Direct Volume declarations:
--------------------
Numeric: supports integer, floating point, 0x, and other hexadecimal prefixes, and ...... And so on;
Boolean value: true/false;
No value: undefined;
Function: function () {...}, also known as an anonymous function;
String: Use '...' or "..." to support multiple lines and escape characters;
Regular Expression: Use/.../to support regular expressions such as g, I, and m;
Array: use [...] to support nested arrays;
Object: {...} is used. nested object declaration is supported;
--------------------
You can use the preceding characters as an entity and use them at any position in the code -- I mean expression or statement line. Just use your nose
The inference is:
// Since it can be written:
Aaa = 'hello, '+ 'World ';
// You can write:
Bbb = [1, 2, 3] + [4, 5, 6];
// You can also write:
Ccc =/abc/+/cdf/
// Similarly:
//......
As shown above, you can place all the direct quantities in the middle of an expression or statement. Occasionally, you may need to use a pair
Parentheses enclose this directly; otherwise, there will be syntax ambiguity, for example, the following:
Ddd = (function () {}) + (function (){})
Well, it's just that simple. You just need to expect that you have a non-degraded nose.
Iv. Prototype inheritance
Prototype inheritance may be the simplest thing in the world.
Let's assume that an object is a table. The great Anders supports this assumption. He said that the JavaScript Object is a "Property package "--
Such a table stores "name/value" pairs such as "name = value. When we want to use the following code:
AObj. name
When you look for a value, check it in the table (people using delphi should remember TStringList ). Object.
In my previous understanding, it was "a struct with an inheritance relationship (struct/record )". So what is the inheritance relationship?
In this case, if the search fails, you only need to find the prototype of the aObj object for prototype inheritance.
This prototype is also an object, which is recorded in the prototype attribute of the constructor function. For example:
Function MyObject (){
//...
}
MyObject. prototype = xxx;
Var aObj = new MyObject ()
Zzz = aObj. name;
When the name attribute cannot be found in aObj, according to the above rules, it will go to the xxx object, that is, try to find "xxx. name ".
Xxx itself is also an object, and there will also be a constructor function (such as xxxObject (), so when xxx. name cannot be found, it will
Go to xxxObject. prototype to find ...... So ...... Such in-depth mining will never be found again ...... Returns undefined.
It's easy. Prototype inheritance is just a simple search rule.
On the other hand, you need to make aObj accessible to a member, and you just need to modify the prototype of it (or they -- are similar to aObj instances ).
That's all. This is very common in JavaScript. For example, you want all strings to have a certain attribute:
String. protoype. MyName = 'string'
For example, if you want all objects to have a certain attribute (or method, or something else), then:
Object. prototype. getMyName = function (){
Return this. MyName;
}
How nice it is. Now the String can also get myname, and the function can also get myname. All the names without names also have names -- of course, the name
The word is undefined.
I didn't know if you would become a philosophical madman. Sorry.

V. Function
This is not the invention of JavaScript. Its inventor is dead, and his invention is still bothering us ...... Like Edison's lamp
Bubbles are still shining on us.
In fact, functional language is very simple. It is a language implementation solution similar to imperative language. Because of its basic ideas and commands
Style-if you don't want to use this difficult term, replace it with C or Delphi-the language is completely different, so most
It is also incompatible with these traditional, universal, and commercialized languages.
In fact, you use it every day.
The following code is full of functional language ideas:
A + B
Yes? Really, if you think of the "+" as a function, it will be exactly the same. In fact, a function is
Execution Process, a piece of operation, a function entry ...... That is to say, something in the Code is either data or computation. For example
If it is an operation, you can regard it as a "function ". The above line of code -- in the expression, a and B are obviously data, and the plus sign is a forward
Therefore, it can be regarded as a "function, process, or operation ". So ...... You can write a "function" that calculates two numbers.
As follows:
Func_add (a, B)
Or:
(+ A B)
All of these are just different markup methods in words. It's like I said this symbol is "jia", and you have to say it is "dark", another
People have to read it as "a-d ". Is there any difference? No.
All programmers write statements, expressions, and operation logic every day. Everyone knows that all these things can be written in one ...... Hmm ...... Extremely huge
It can be divided into countless small and pretty functions. When all these functions are operated consecutively
It becomes a functional language ". Therefore, LISP, known as the "grandfather of functional language", is the language that "connects function operations:

The Code is as follows:


(Defun subst (x y z)
(Cond (atom z)
(Cond (eq z y) x)
('T z )))
('T (cons (subst x y (car z ))
(Subst x y (cdr z ))))))


Some people think it is ugly pasta, and some people think it is the most concise language. Oh, you just need to know about it. You just need to know what it is like.
Function language only has functions that are executed consecutively. Therefore, it does not have a "statement" and must implement logical integrity. Simply put, he
The "sequence, branch, and loop" logic must be implemented in the continuous execution process. The solution of the function is to use the Trielement conditional operation.
Replace if/then/else with the following code:
If a then B else c
// Equivalent
A? B: c
In addition, recursion (function tuning) is used to implement loops. Considering that recursive function calls may cause Stack overflow (Stack overflow ...),
Therefore, functional languages propose "tail recursion", that is, when writing code, it is "guaranteed" that only recursive calls are made in the last operation of the function.
In this way, this recursive call does not need to retain the stack-because there is no subsequent operation, it can be optimized into a row without returning
Jmp Assembly command.
The world is really beautiful. The so-called functional formula is just a bunch of computing portals, as well as jmp, jmp, and jmp. But, isn't it?-This CPU?
6. More advanced functions (1)
In fact, the world is not beautiful.
If a piece of CPU is dead there, it will only contain commands such as order, branch, and loop. But when it runs, there must be
Clock is ticking... Tick... If it is multi-core, there will also be several such things dropping.
This is the problem. The world is neither a single sequence nor a sequential sequence. Therefore, there will be concurrency, interruptions, and exceptions. Functional Language
I should extend myself like an Italian noodle without limit. Should I have more pasta when there are multiple clicks? However
Is it also called pasta?
The solution in the function formula is Continuation and node. The continuation is to stop the current conversion to another place, and then return back; and the node is indeed
The Code on one node is independent and complete, and has nothing to do with another node. In functional languages, multiple nodes are like parallel universe.
In this case, they are transparent to each other. If they need to be connected, they need a huge amount of energy and ...... A wormhole.
We do not need to go into the issue of multiple nodes. The access to multiple spaces brings about the space-time problem. It is the research of the astronomical scientists and the space-time and multi-dimensional theory.
If you want Cross-access relationships between multiple nodes, the world/universe will be destroyed. Such class
A similar description is written in White Papers, reference manuals, and routine guidance for astronauts, which are natural languages that support multiple spaces such as ErLang. If you want to be poor
The root cause is that you have been able to clearly understand more than 300 advanced digital, physics, and astronomical terms.
The entry guide of the sequencer may start from here:

Https://www.php1.cn/
Continuation is one of the solutions to the status issue. Simply put, there is a state with time: past, present, and future. For the past, we have a history, and we will decide what happened in the past. For example, we can only eat porridge today because we drank high wine yesterday, so we need to record the status of today for what we can do tomorrow, for example, we have already eaten porridge today. As for tomorrow, of course, there will be many things to happen, so we have to make preparations one by one.
Oh, the important thing is that we are ready. This is called an event in a computer system. It can also be called a plan or an emergency. Do you know why our PC can run? En... is because there is a command line that executes commands in a small enough time slice. For the computing system, the scheduled command access behavior is interrupted. One day, a friend asked me: if the execution logic is only sequential, branch, and loop, what are the triggers in the process system? I have been thinking for a long time. Now, to answer this question, we need to add the "time" dimension. The so-called bursts, concurrency, and similar things are the logic of the time series concept.
Well, when we are "ready" to prepare for triggering something in the future-simply put, it's a clock handler (in windows it's called OnTimer, in the browser, it is called setTimeout/setInterval). In order to make a pasta meal in the functional language at this time, we note: "We are ready ", and "How are we prepared ". According to the basic principles of the functional formula, a function is irrelevant to the State, and it is irrelevant to the future time series. This becomes a conflict.
In fact, we have already been in a conflict with this conflict. This is a "loop ". Because the loop requires a number of States to indicate the loop progress,
This "progress" is time series related. The function method uses "recursion" to solve it, that is, the progress is passed through the parameters of the recursive function. On both sides of the "recursive Parameters"-interface, the function type is time series independent. The resulting problem is stack overflow, and the solution is tail recursion. The resulting problem is programming complexity and whether tail recursion can replace all recursion. The solution is ...... That's right. All solutions to the problem will bring about new problems. Oh... It's Winnie. Now we need "more States", because we have run one or more time series of the system-that is, the CPU. Even if we have nodes and ensure that there is no wormhole, we also need to solve the past, present, and future problems of a CPU.
Function-based xianjia said two words: continuous. Simply put, it is to pass the past and present statuses and future preparations as function parameters. It seems that "now" is about to explode immediately, because it involves both the past and present states and changes, and future operations. So the new solution is: do not perform operations on the "continuous" interface if you do not want to explode now.
Operations are performed by "future" based on "past" data, which is sustained. The functional feature that supports it is to evaluate it in inertia. To put it simply,
Function f (x, c ){
...
C (x );
}
F (data, f );
Because c itself does not ask for a value on the transfer interface f (), the explosion will occur when c (x) performs operations in the future. If
The entropy of the universe has a limit, so this limit is also known in the future. Moreover, in the foreseeable future, it may be distorted back to the present. This is
Two principles of continuity:
--------------------
A continuous success is a new continuous (or process)
If a failure persists, it will return to the previous selection point.
--------------------
It is precisely because there is a continuous state, and this State and continuity itself are passed through the function parameters, so "back to the Choice Point" is only for the future itself
Is irrelevant to the current status.
7. More advanced functional formula (2)
In any case, the complexity of various functional patterns does exist in the programming paradigm to maintain a kind of self-purity. For example, generator (generator)
This problem occurs because the generation of a batch of data is related (for example, incremental data), and the generation process is time series related (not always in a single call ).
So the functional language defines a function concept such as "generator. We can define a yield in the generator,
The returned result is a wormhole hole in the "future" to return to the "present. Through this wormhole hole, we can get a time series related data. Below
This is an example (sample of the Fibonacci series on mozilla ):

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.