Show toolbar

2013年5月1日 星期三

Fluent interface in JavaScript

標題:JavaScript的流暢介面寫法
JavaScript (main.js):
1
2
3
4
5
6
7
8
9
10
11
12
13
var brain = function() {
this.echo = "I am ";
this.word = function(name) {
this.echo += name;
return this;
};
this.say = function() {
alert(this.echo);
return this;
};
};
var brain = new brain();
brain.word("QQBoxy").say();

說明:
在jQuery中常看見流暢介面(Fluent interface)的寫法,
這對程式的理解上有著極大的幫助,
如同範例程式中大腦(brain)要處理文字(word)然後講出(say)我要講的話,
如此一來便能非常直覺了解程式所要執行的動作。