No nonsense JavaScript (on)

Source: Internet
Author: User

"Programmer" No. 2008.09 has a "no nonsense Erlang" article, which reminds me of a lot of such as "no Nonsense C", "No
Nonsense book review "This kind of article, also think of JavaScript can not a" no nonsense ", so decided to open an article to write this. With this
Decide on the relevant, simply, this time write the simplest one.

Make a statement: If you just want to see something complicated, don't read this article.


First, JavaScript is actually a process-

JavaScript, which dates back to the 1.0 era, is in fact procedural. There are only two basic features, one is the HTML that can be placed directly on the Web page
tag to take over the event, for example:

[XHTML]View Plaincopy
    1. <input type="button" value="Go" onclick="alert (' Hello ')">


The second is to support a simple object constructor (function). In fact, the constructor of this era is more appropriate than the initialization function, it should
Write this:

[JavaScript]View Plaincopy
    1. function MyObject () {
    2. this.xxx = ' ... ';
    3. this.yyy = ' ... ';
    4. }
    5. obj = new MyObject ();

As a result, early JavaScript undoubtedly backs up the "object-based procedural scripting language" header, which is not an injustice. Apart from
In the above two features, JavaScript has some general scripting language properties, such as:
-The entire. js file is loaded into the execution environment (such as a Web browser) once, and after a parsing, the line execution begins;
-In the above parsing cycle, the (named) function and the variable declared with "Var" are pre-processed in an identifier table for use by the script code;
-Executes from the global code line or function call, and the code that is not executed during the entire process is not checked for errors (except for syntax errors in the first step).
It also has the nature of the usual procedural language, such as:
-There are if/for/while/switch and other statements;
-Use function to declare functions, using "(..)" To declare its formal parameter table, and to represent function calls and arguments;
-Similar to the basic syntax of C, including the use of "{...}" To represent blocks of code and to use arithmetic symbols such as "! =";
-An object manipulation operator similar to the Java language "." and the basic concepts of attributes, methods, and so on.

Well, now that you see a basic JavaScript language, its code is just like C-like functions and statement lines that support very simple polygons
Programming to Objects. OK, this is actually almost all of javascript ... Well...... All the basic concepts of grammar. If you ever use a door even
A little bit of programming language, you will find JavaScript is actually quite simple.

Yes, "write a function and call it", it's that simple. For example:

[JavaScript]View Plaincopy
    1. function Hi () {
    2. Alert (' Hello, world ');
    3. }
    4. Hi ();



Second, a little more complicated is the data type

JavaScript has six basic data types, divided into two categories. A class is a value type, that is, undefined,string, number, and Boolean;
is a reference type, that is, function and object. Detects what type of data x is, and can simply use "typeof X" to return a string.

Value types and reference types in other high-level languages are distinguished by "pass-through or reference-to-pass" during access. Simply put, in the following function
In

[JavaScript]View Plaincopy
    1. function foo (X) {
    2. }

X is the value itself, or a reference to the value (which you can imagine as a pointer), indicating what type X is. Different from other languages
Yes, JavaScript does not add an indicator word to the call entry to illustrate the method of passing values, for example:

[C-sharp]View Plaincopy
    1. function foo (var X) {
    2. //General high-level language, Var indicates that references to variable x are always passed in
    3. }


Instead, the scripting engine determines how to pass values based on the data type of the actual incoming x. For example:

[JavaScript]View Plaincopy
    1. function foo (X) {
    2. ...
    3. }
    4. Foo (' 123 '); //<-string ' 123 ' value
    5. Foo (aobj); //<-Aobj is an object reference


The key to this approach is that the JavaScript type system is concise enough. Six basic types include three philosophical concepts: what can be done
Object or non-object, with (value) or none (value). Clearly, it is not easy to understand this philosophical idea, because the more complex one layer
, self-contained logic is: A function is also an object, a value is an object, no value is also a value.

This is all of the JavaScript type system. If you want to use it simply, remember that the following is enough:
-string, Number, Boolean three simple value types are used to pass to the Web page display;
-object is used to store other object, funtion, or the above simple value type and use '. ' Operations are found by a property name;
-undefined is used to detect valid invalid data;
The-function is used for execution.
Of course, if you want to be a thinker or a lunatic in linguistics, then think about the philosophical proposition above and I won't stop you.


Three, can use the nose to figure out is the direct quantity

Maybe a lot of people don't understand the direct volume declarations in JavaScript, but it's really very simple. Since most of our high-level languages are
Constant declarations are supported, and even the most primitive assembly language supports immediate values-for example:

[C-sharp]View Plaincopy
    1. In C
    2. #define ABYTE 256
    3. In Delphi
    4. Const
    5. Abyte = 256
    6. ; In ASM
    7. mov ah, 256

So JavaScript of course ... Inevitable...... It is possible to support direct quantities without shame-they are actually a concept. For example:

[JavaScript]View Plaincopy
    1. In JavaScript
    2. Var
    3. Abyte = 256

But in understanding, be sure to remember: All of the above code, the so-called direct or immediate value, refers to the ' 256 ', not that
The identifier for a variable or constant abyte/abyte. Further, you need to know that JavaScript supports 8 direct volume declarations:
--------------------
Numeric values: support for integers, floating-point and 0x prefixes, and ... And so on
Boolean value: True/false;
No value: undefined;
Functions: Function () {...}, also known as anonymous function;
String: Using ' ... ' or "..", supports multirow and escape characters;
Regular expressions: Use/... /.., support g,i,m and other regular configuration;
Array: Use [...] to support nested arrays;
Object: Use {...} to support nested object declarations;
--------------------
You can use the above character as an individual, anywhere in the code-I mean an expression or a statement line. You can do it with your nose.
The inference is:

[JavaScript]View Plaincopy
    1. Since you can write:
    2. AAA = ' Hello, ' + ' world ';
    3. Then it is necessary to write:
    4. BBB = [n/a] + [4,5,6];
    5. It is also necessary to write:
    6. CCC =/abc/+/cdf/
    7. Same:
    8. ......


As above, you can put all the direct amounts in the middle of an expression or statement. Occasionally, because of the need for grammatical parsing, you may need to use a pair of
Parentheses enclose this direct amount-otherwise there is a syntactic ambiguity, such as the following:

[JavaScript]View Plaincopy
    1. DDD = (function () {}) + (function () {})

OK, the direct volume is so simple, you just have to expect yourself to have a not degenerate nose.


Iv. prototype inheritance

Prototype inheritance may be the simplest thing in the world.

We assume that an object is a table--The great Anders supports my hypothesis, and he says that the object of JavaScript is "property bag"--
Such a table is stored in "name=value" such as "name/value" pairs. When we want to use the following code:

[JavaScript]View Plaincopy
    1. Aobj.name

To find the value, look it up in the table (People with Delphi should remember Tstringlist). Objects, oh, so-called objects--in
I used to understand that "struct with inheritance (Struct/record)". So, what is the inheritance relationship?

In this case, if the above lookup is unsuccessful, for prototype inheritance, just look in the "prototype" of the Aobj object and it will be done.
This prototype is also an object that is recorded in the prototype property of the constructor function. For example:

[JavaScript]View Plaincopy
    1. function MyObject () {
    2. // ...  
    3. }
    4. Myobject.prototype = xxx;
    5. var aobj = new MyObject ()
    6. zzz = Aobj.name;


When the name is not found in the Aobj property, according to the above rules, will go to the XXX object to find, that is, trying to find "xxx.name".
Because XXX itself is also an object, there will also be a constructor function (such as xxxobject ()), so when Xxx.name is not found, it will
Go xxxobject.prototype inside to find ... So...... So deep digging, until you can no longer find ... return to undefined.

How simple, the so-called prototype inheritance, is simply a simple search rule.

Conversely, you need to have aobj access to a member, and you only need to modify it (or their--a reference to a aobj-like instance).
Just fine. This is very common in JavaScript. For example, you want all strings to have a property:

[JavaScript]View Plaincopy
    1. String.protoype.MyName = ' String '

For example, if you want all objects to have a property (or method, or something else), then:

[JavaScript]View Plaincopy
    1. Object.prototype.getMyName = function () {
    2. return this .  MyName;
    3. }

How beautiful, now string can getmyname, function can also getmyname, all have no name also have name--of course, name
The word is undefined.

No name is a name, I did not think you will become a philosophical lunatic, sorry.

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.