入口视频地址: http://video.yahoo.com/watch/111593/1710507
PPT 下载地址:http://yuiblog.com/assets/crockford/javascript.zip
适合写过javascript程序,却一头雾水中的所有人
三人行,必有我师,我相信每看一遍,都会对javascript有更进一步的理解
只能说,临渊羡鱼,不如退而结网
笔记:
Key Idea of Javascript:
Load and go delivery
Loose typing
Objects as general containers
Prototypal inheritance
Lambda
Linkage though global variables
基本数据类型:
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 should 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 to
var foo = function foo() {};
An inner function has access to the variables and parameters of functions that it is contained within.
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+/S+)*)/s*$/, "$1");
};
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' ?
r : a;
}
);
};
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 evil
Why inheritance? :
Automatic casting
Code reuse
if (!a) { ... }