高手提領 之 Douglas Crockford: The JavaScript Programming Language

來源:互聯網
上載者:User

入口視頻地址: 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

 

 

基礎資料型別 (Elementary 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 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) { ... }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.