A friend recommended book (http://eloquentjavascript.net/), while not busy recently read the next.
In general, this book is not suitable for beginners of JS, because the examples in it are relatively poor, less academic, and practical applications.
I personally recommend head first JavaScript For JS books. In fact, it is said that all the books in the head first series are good.
Every book has its merits. If you plan to read this book, you may just take a look at my notes...
Note: The English text and Chinese text are my comments. I directly translated Chinese without English.
1, there are six basic types of values: Numbers, strings, booleans, objects, functions, and undefined values.
6 basic types of Values
2, don't forget the special unary operator (typeof)
Easy to forget, unary operator typeof
3. In JS, the end of a statement generally includes a semicolon, which can be ignored or sometimes required. We strongly recommend that you retain the semicolon.
(Reader Note: it is customary to retain semicolons. It is not prone to errors when using Yui compressors to compress JavaScript code)
4, undefined and null.
If the variable value is null, the variable is defined but has no value. If the value is undefined, the root is not defined. But null = undefined returns true. So in JS, if the variable is to be compared with null or undefined, it is better to use = or! =
5. other strange phenomena in Js.
VaR x = false = 0; var y = "" = 0; var z = "5" = 5; XYZ is true
Reader Note: There are many strange phenomena.
6. Exact equals and exact not equal to = and! =
Use = and! = During the comparison, the JS interpreter will try to convert the type (for example, some strange phenomena in 5). If you are not familiar with the automatic type conversion during the comparison in JS, then use = and! =
7. Guess what Nan = Nan is? False...
8, | and & operator. True | X, X will never be executed; similarly, false & Y will never be executed.
| It is often used. & I have never tried it myself
9. What is returned by a method without a return statement?
Undefined
10, functions are the only things that create a new scope
Scope ~ This statement means that when a block-type Statement (such as if, while) has a life variable, the scope of the variable is actually the function of the block statement. This is quite different from C #. It is said that the new JavaScript version will change this point.
11, in Javascript, running through a simple loop is a lot cheaper than calling a function multiple times.
For a simple for or while loop, the performance is much better than the recursive call of the function. However, if the logic is complex, recursion should be used. performance should be considered, but performance should not be overly worried unless yourProgramIt's really slow.
12, the computer must remember the context from which the function was called, so that it knows where to continue afterwards.
The place where this context is stored is called the stack.
Stack is the place where the context of the function Runtime is stored. When a function is called, the context associated with the function is in the head of the stack. After the function is called, the context is removed from the stack, at the same time, continue to process the context of the remaining functions in the stack.
The physical nature of the stack is manifested in the memory usage of the computer. If the stack is too large, the computer will throw "Stack Overflow out of stack space" or "excessive recursion too much recursion" information. Therefore, once you encounter these two prompts, remember to check the program where recursive calls lead to an endless loop.
In jquery, the combination of the animate method and the Stop method can easily lead to an endless loop. If you call an animation with a callback function for element $ X, for example, $ X. animate ({XXX}, 400, callback), and then calls $ X. stop (True, true). Pay attention to your callback function at this time.
13, numbers, booleans, the value null, and the value undefined do not have any properties
Numbers, Boolean values, null, and undefined have no attributes
14, the keyword Delete cuts off properties
The delete keyword is used to delete the attributes of an object.
15. The operator in can be used to test whether an object has a certain property. It produces a Boolean
The in keyword can be used to check whether an object has a specified attribute.
For example, VAR Levin = {Name: 'levin '};
Alert ("name" in Levin );
16, String. Split (x). Join (x) always produces the original value, but array. Join (x). Split (x) does not.
17, charat will return "" when there is no character at the given position, and slice will simply leave out the part of the new string that does not exist.
18, the properties of math are hidden
For (VAR name in math)
Print (name );
Nothing happened.
19, the simple objects we have used so far are based on the most basic prototype, which is associated with the object constructor. In fact, typing {} is equivalent to typing new object ()
The constructor attribute of the prototype object and the literal object points to the constructor of the class that creates the object. The constructor attribute of the literal object points to the constructor of the object class.
Reader Note: can refer to my example: http://www.vivasky.com/labs/jqfocus/js_PrototypeBasedObject_vs_LiteralObject.html
20, every function automatically gets a prototype property, whose constructor property points back at the function
Any class has a prototype attribute. The constructor attribute of this attribute points to the function itself that declares this class!
21, the properties of the prototype influence the object based on it, but the properties of this object never change the prototype
The attributes of the class prototype affect the objects of the class, but the attributes of the Class Object do not change the class prototype.
VaR baby = function (name) {This. Name = Name ;};
Baby. Prototype. Age = 1;
VaR baby1 = new baby ("cocoa ");
Baby1.age = 2;
Alert (baby. Prototype. Age);/* still shows 1 */
22,If your program has to run on the same web-page as another program (either written by you or by someone else) which uses for/In naively-the way we have been using it so far-then adding things to prototypes, especially the object and array prototype, will definitely break something, because these loops will suddenly start seeing those new properties. for this reason, some people prefer not to touch these prototypes at all. of course, if you are careful, and you do not have CT your code to have to coexist with badly-written code, adding methods to standard prototypes is a perfectly good technique.
Try to avoid prototype extension for JS local classes such as object and array.
Reader Note: You have encountered such problems at work.
23, modularity
When structuring a program, we do two things. We separate it into smaller parts, called modules, each of which has a specific role, and we specify the relations between these parts.
JS programming organization-modular Development
By function modules, each module is responsible for the role (function ). The communication mode between modules is the key. You can use the event mechanism in Java or C # for reference. If you are using jquery, it is better to deal with it because it provides a complete set of event processing mechanisms.
When organizing various modules of a program, we should avoid circular dependencies between modules as much as possible. For example, if a depends on B and B depends on a, it may lead to logical confusion.
Jquery also adopts modular Development Since version 1.4.3, for example, putting the processing effect function into effects. the JS module handles Dom traversal in the core. JS and so on. Since the modules are as independent as possible, you can build your own jquery version as needed.
During modular development, it is recommended to physically separate each module into different JS files, and then combine them into a JS file using the Yui compression tool during deployment.