2007/12/31

JS OO programming筆記

原本我們都是使用JS framework的包裝,但在這兩篇說明javascript對OO的支援方式的文章中,我們可以看到最原始的javascript

Part I: Inheritance http://www.webreference.com/js/column79/

Part II: Methods http://www.webreference.com/js/column80/

OO的基本特性 inheritance, encapsulation, polymorphism

提供 objects, prototypes, implicit inheritance,沒有classes, instances

prototyping 是瞭解inheritance的核心概念

以js定義function,在function中定義methods

保護 private field data element

context, scope, context switching

***********************

OO的特色 encapsulation 封裝(data and methods), inheritance 繼承, and polymorphism 多型, class, superclass, subclass, override

js 不直接支援inheritance,有兩種方法

1. 用function來實作

function superClass() {

this.bye = superBye;


this.hello = superHello;


}

function subClass() {

// 這兩行用來定義superclass


this.inheritFrom = superClass;


this.inheritFrom();


// override bye這個 method


this.bye = subBye;


}

2. 用prototype

function superClass() {

this.bye = superBye;


this.hello = superHello;


}

function subClass() {

this.bye = subBye;


}

// 這裡用prototype指定 superclass

subClass.prototype = new superClass;

************

object1.prototype.isPrototypeOf(0bject2); 判斷object2裡面是否有object1為prototype

netscape: 以 __proto__ 紀錄 prototype物件

************

用__proto__來模擬 instanceOf

//for NS only

function instanceOf(object, constructorFunction) {

while (object != null) {

if (object == constructorFunction.prototype)

{return true}

object = object.__proto__;

}

return false;

}

************

Object object supports the constructor property

************

js 支援三種object: native, host, and user-defined

native: js 語言支援的物件,包含 Object, Math, and Number

host: browser在載入頁面時產生的物件,包含 document, window, and frames

user-defined: 自訂物件,case-sensitive

The Object object is the base object for all native and user-defined objects. All objects inherit from this superclass.

Object的內容

1. property: constructor

2. methods: isPrototypeOf, hasOwnProperty, toLocaleString, toSource, toString, valueOf

propertyIsEnumerable

*****************

列印 物件property

for (property in ken) {

alert(property);


}

*****************

產生物件

function Employee() {

var registerA = "Initial Value";

function setRegisterA(param) {

registerA = param;

}

this.setRegisterA = setRegisterA;

this.dept = "HR";

this.manager = "John Johnson";

}

***************

定義function的三種方法

1.

function first(param) {

alert(param + " method to define a function");


}

2.

second = function(param)

{alert(param + " method to define a function")};


3.

third = new Function("param",

"alert(param + ' method to define a function')");


****************

Functions in javascript can modify global variables from the outer context.

定義method的時候,就會希望是使用private data member,第三種方法(用Function)會產生錯誤,

所以只能用第一與第二種建立function的方法

function myGlobal() {

var global = 5;


function first(param) {


global += 10;


alert(param + " method shows global to be " + global);


}


this.first = first;


this.second = function(param) {global += 10;


alert(param + " method shows global to be " + global)};


this.third = new Function("param", " global += 10;


alert(param + ' method shows global to be ' + global)");


}

var x = new myGlobal();

***********************

定義Context

有三種context: Global code, Eval code, and Function code

當browser進入一個新的context的時候,就會建立新物件(variable object),保存新context的變數與function,當browser跳出此context的時候,variable object就會被刪除

global code: 在functions外面執行的codes

eval code: 是透過eval function執行的code

function code: 是在function定義中的code

當browser切換context的時候,就會決定新的scope,進而決定可以存取哪些variables, objects與functions

************

判斷scope

global code的scope包含了兩種物件: global object and variable object

global object就是window這個variable物件,也可以用this來代表window,ex: window.location也可以寫成this.location

eval code的scope等同於calling code的scope,包含global and variable object

function code的scope包含

(1) calling code's execution context的variable objects,包含該function被呼叫時,所定義的所有的variables

(2) outer function的variable object

(3) global object,也就是window

(4) arguments object,包含了所有參數

通常this就代表window,但在object method中,this就是這個method所屬的object

ex:

function ObjectConstructor() {

// 這裡的this就不是window


alert(this.location);


}

// 物件

function createObj() {

// method


var newObject = new ObjectConstructor();


}

***********

強制建立object 的方法

function Employee(a) {

if (!(this instanceof Employee)) return new Employee(a); // 如果這行沒寫的話就會error


this.name = a;


}

function init(){

John = Employee("Johnson"); // 沒有寫 new


alert(John.name);


}

*********

在subclasses之間共用private data

function Shape() {

var area = 50;


this.setArea = function(a) {area = a;};


this.getArea = function() {return area;};


}

function Square() {

}

Square.prototype = new Shape();

var shape1 = new Square();

var shape2 = new Square();

shape1.setArea(100);

****************

保護private data的兩種方法

1. 用call()

function Shape() {

var area = 50;


this.setArea = function(a) {area = a;};


this.getArea = function() {return area;};


}

function SquareB() {

Shape.call(this);


}

var shape1B = new SquareB();

var shape2B = new SquareB();

shape1B.setArea(100);

//shape2B's 的area變數就不會被影響還是50

2. call a constructor from within a constructor

function Shape() {

var area = 50;


this.setArea = function(a) {area = a;};


this.getArea = function() {return area;};


}

function SquareA() {

this.Shape = Shape;


this.Shape();


}

var shape1A = new SquareA();

var shape2A = new SquareA();

shape1A.setArea(100);

//shape2B's 的area變數就不會被影響還是50

2007/12/26

轉轉球迷宮 130關 100% 紀念

這是個好玩的遊戲,需要穩定的雙手控制Wii Remote(單手拿比較不穩),但偶爾也能耍些小技巧,例如第一部份最難的第E關,要在那麼小的路上走滑水道,簡直是天方夜譚,就直接想辦法讓球從某一個中繼點跳到另一個中繼點,事實上也要感謝遊戲設計者沒有那麼刁難我們,因為可以一直接關,比較難的關卡設計了中繼點。

大多數特殊的球都沒有用,只要用一開始的彈珠就行了,只有為了要拿25個金盃的時候,有時候就會用排球(重量輕、速度快、但會彈跳)、或是土星(重量重、穩定、速度快)。

前50關

前50關的鏡像關

後15關

後15關的鏡像關

所有的球

2007/12/18

Ghost Squad Level 99 紀念

前一陣子唯一的娛樂,就是每天晚上拿槍掃射,這是Level 99的紀念。其實這個遊戲也沒什麼,就是一直開槍跟接關而已,反正也是消磨時間。

Level 99

元帥的Gold服裝,和一把貫穿能力高,100發連發的機槍


三個關卡都是Lv16

熊貓裝的正面

熊貓裝頭部


熊貓裝背後的那隻貓

2007/11/25

得獎的童書比較不會踩到地雷

有一陣子沒有大量地買書了,最近用網路購物買了不少,有些中文書就到「販讀書店」去訂。

在網路上購書,因為不能看到內容(事實上有些書店會把書本裝在塑膠袋裡,也是不能試讀的),除非買的書有口碑、有名氣,就不用擔心作品的品質如何,可以勇敢地刷下去,這個時候,參考是不是得獎作品這個指標,就蠻好用的,網路上也有人列出百大繪本或是圖書的列表,這也是個參考的指標。

在這個網站http://store.pchome.com.tw/mackids/S037251.htm一次買了六本得獎的書(因為要滿一千元才不用運費><):

‧ Click Clack Moo
‧ 〈凱迪克銀牌獎〉Snow
‧ There was an Old Lady Who Swallowed a Fly
‧ ﹝凱迪克大獎﹞YO! YES!
‧ Don't Let the Pigeon Drive the Bus!

‧ 〈James Marshall〉Goldilocks and the Three Bears

六本書才沒幾天就送到家裏,真的很方便,小朋友也迫不及待地,一下子就聽了五個故事,第六個故事字太多了,小朋友的注意力也不集中了,馬上停住,休息囉。

2007/11/5

去跟媽媽講,爸爸喝醉了

2007年11月3日,爸爸第一次喝醉了,奇怪的是,爸爸是很「清醒地」喝醉了。

晚上飯後,很久沒有喝酒的爸爸,開了一瓶750ml 14.5%的白酒,配上多力多滋,一邊吃一邊喝,我也很興奮地跟著爸爸一起吃零食。

爸爸才喝到1/3,就跟媽媽說,他的頭有點暈,爸爸還是一直喝,我們還一邊看著電視裡的人,在比賽魔術表演。接下來媽媽可能因為白天工作,很累地睡在沙發上,我也不大想看電視,爸爸已經喝了2/3,還沒停下來。

我跟爸爸說要玩心臟病,我們坐在和室地板上玩撲克牌,爸爸還是很清醒地,跟我一起玩牌,他還能控制自己什麼時候要放水,不過我發現,爸爸去上廁所的時候,扶著牆壁搖搖晃晃地。

時間到了,我該洗澡去,爸爸已經把酒喝光了,爸爸要我準備洗澡,他搖搖晃晃地到了房間拿衣服,跟平常一樣,幫我洗澡,還問我要不要講故事,我當然說好,接著,我選了兩本書在床上聽爸爸講故事。

爸爸很清醒地,幫我講了兩本故事書,「貓咪數不完」跟「等我一分鐘」,爸爸一樣搖搖晃晃地走到他們房間,還沒到浴室的時候,爸爸就吐了,地上多了兩攤東西,好臭,爸爸跟我說「去跟媽媽講,爸爸喝醉了」。

媽媽醒過來了,幫我刷牙,我要回房間的時候,發現爸爸自己已經用抹布,把地板清理乾淨,只聽到他跟媽媽說,我喝醉了,我自己整理好了,要睡覺了。

隔天中午,爸爸說他還有一點點頭暈,這應該可說是宿醉吧,下次別再喝酒了,你已經不會喝酒了,爸爸。