What
Is
Node.js
(Javascript)
o A
script
language
o A
dynamic
programming
language
o Easy
to
learn
o Extremely
difficult
to
master
o Running
outside
of
the
browser
context
var
a
=
{}
相當於 Object.create(Object.prototype)
o 產⽣生⼀一個物件
o 指定Object.prototype
為物件的 prototype
o empty
constructor
18.
var
c
=
new
Claz()
產⽣生物件時,⽤用 Claz.prototype
做為物件的
prototype,並呼叫 Claz()
來起始物件:
o var
c
=
Object.create(
Claz.prototype
);
o Claz.call(
c
);
19.
以FuncYon實作 Class
var
MyClaz
=
funcYon(t)
{
var
Ytle
=
t,
//
this
is
a
private
variable
language
=
‘zh’;
this.getTitle
=
funcYon()
{
return
Ytle;
};
this.getLanguage
=
funcYon()
{
return
language;
};
};
var
o
=
new
MyClaz(‘Hello
KSDG’);
console.log(o.getTitle()
);
20.
夠⽤用,但缺了什麼…
o 能不能有
staYc
的變數?
o 把函數都直接定義在物件
(this)
上,會吃
掉較多的記憶體和
CPU
cycles
21.
仿物件導向的程式樣板
var
myClass
=
(funcYon()
{
var
sPrivVar;
//
this
is
a
staYc
private
variable
var
MyClaz
=
funcYon()
{
var
privVar;
//
this
is
a
private
variable
this.method1
=
funcYon()
{
/*
do
whatever
you
need
*/
};
};
MyClaz.prototype.method2
=
funcYon()
{
/*
a
class
method,
too
*/
}
return
MyClaz;
})();
module.exports
=
myClass;
以 coServ 為範例
o coServ:
新世代的
web
server
o 參考資料:
o h`p://www.slideshare.net/BenLue/web-‐
server-‐co-‐serv
o h`p://www.slideshare.net/BenLue/web-‐
server-‐coservparYi
o 利⽤用本地模組做練習
o Live
demo