Portal video address: http://video.yahoo.com/watch/111593/1710507
PPT: http://yuiblog.com/assets/crockford/javascript.zip
Suitable for anyone who has written javascript programs but is confused
I believe that every time I read it, I will have a better understanding of JavaScript.
It can only be said that Linyuan Yu is better to move away from the Internet
Notes:
Key idea of javascript:
Load and go delivery
Loose typing
Objects as general containers
Prototypal inheritance
Lambda
Linkage though global variables
Basic Data Type:
Number:
No integers!
64-bit floating point, IEEE-754 (double)
NAN:
Not a number
Toxic: Any rithmetic operation with Nan as an input will have Nan as a result
Nan! = Nan
Type (NAN) = Number
Parseint (value, Radix)
:
It stops at the first non-digit Charactor
The radix shoshould be required
Parseint ('08') = 0
Parseint ('08 ', 10) = 8
Strings:
Sequence of 0 or more 16bit charactors
UCS-2, not quite UTF-16
No awareness of surrogate Pairs
No seperate charctor type
Characters are represented as strings with a length of 1
Strings are immutable
Similar strings are equal
String Literal can use single or double quotes
Undefined:
A value that isn' t even that
The default value for variables and parameters
The value of missing members in objects
Falsy values
:
False, null, undefined, "", 0, Nan
Others are truly: "0", "false"
Key are lowercase
+
'$' + 3 + 4 = '$34'
Unary operator can convert strings to numbers
+ "42" = 42
+ "3" + (+ "4") = 7
= /! =
Thses operators can do type coercion
It is better to === ,! ==, Whcich do not do type coercion
&&:
& The guard operator, aka logical and
If the firsit operand is truthy
Then result is second operand
Else result is first operand
Return A & A. Member
|
If the firsit operand is truthy
Then result is second operand
Else result is first operand
Bitwise
The bitwise operators convert the operand to a 32-bit singed integer, and turn the result back into 64-bit floating point
Slow!
For loop property of Object
For (VAR name in object ){
If (object. hasownproperty (name )){
...
}
}
Return:
No void Function
Create new object:
New object ();
{}
Object (object. Prototype );
Array:
Array inherit from object
Indexes are converted to strings and used as names for retrieving values
Very efficient for sparse Arrays
Not very efficient in most other case
No need to provide a length or type when create an array.
Arrays Unlike objects, have a special length Member
It is always 1 larger than the highest integer subscript
It allows use of the traditional for statement
Do not use (for in) with Arrays
Function:
The function statement is just a short-hand for a var statement with a function value.
Function Foo (){}
Expands
VaR Foo = function Foo (){};
An inner function has access to the variables and parameters of functions that it is contained.
This is known as static scoping or lexical scoping.
The scope that an inner function enjoys continues even after the parent functions have returned.
Fade two things, each of function involved has its own scope,
If a function is called with too few arguments, the missing values will be undefined.
String. Prototype. Trim = function (){
Return this. Replace (
/^/S * (/S +) *)/S * $/, "$1 ");
};
String. Prototype. supplant = function (o ){
Return this. Replace (/{([^ {}] *)}/g,
Function (a, B ){
VaR r = O [B];
Return typeof R === 'string '?
R:;
}
);
};
Great template:
New Function (parameters, body)
Yahoo. Trivia = function (){
// Define your common vars here
// Define your common functions here
Return {
Getnextposer: function (CAT, diff ){
...
},
Showposer: function (){
...
}
};
}();
Global:
On browsers, window is the global object.
Global variables are edevil
Why inheritance?
:
Automatic Casting
Code reuse
If (! A ){...}