1. 程式人生 > >精通javascript study

精通javascript study

2.1.3 作用域

作用域由函式劃分,而不是由塊劃分

2.1.4 閉包

2.1.5 上下文物件

程式碼總是有一個上下文物件(程式碼處在該物件內)

上下文物件是通過this體現

2.2 面向物件基礎

Javascript程式設計新手常見弱點在於按照功能編寫程式碼,而不考慮上下文或組織

2.2.1 物件

物件是JS的基礎,這門語言裡所有東西都是物件。

2.2.2 物件的建立

JavaScript沒有類的概念 其他程式語言需要例項化某個類,JS不用 JS物件可以用來建立新物件,而物件也可以繼承自其他物件。這個概念稱為原型化繼承 對應建立新物件的方法,JS的做法是,任何函式可以被例項化為一個物件。 (leo:  其他OO語言強調屬性與與操作屬性的方法的封裝,重點在屬性,而這裡強調函式,而函式存在呼叫關係(上下文??),所以,作用域由函式(物件??)劃分, 這便是所謂的函式式語言麼。)

It's all about where you are.

In all programming languages, there is this idea ofcurrent scope andcurrent context. In JavaScript we have a lexical scope and a current "this" context.

In JavaScript all new scopes are created through "function" definitions. But contrary to other c-like languages,this is the only way to make a new scope

. For loops don't do it, if blocks don't do it, plain curly braces assuredly don't do it.


JavaScript comes with a couple of handy-dandy functions on Function.prototype called call and apply

Var statements

The var statement is really just a keyword to specify which nested scope a variable applies to. In fact, if you never used var

, then all your variables would beglobal and walk all over each other.