學好 node.js 不可不知的事

28
學好 node.js 不可不知的事 Node.js -- 程式設計師的魔術棒 Ben Lue

Upload: ben-lue

Post on 27-Nov-2014

1.365 views

Category:

Software


0 download

DESCRIPTION

8/30 在 KSDG 分享的內容。除了說明何謂模組之外,也介紹 node.js 從初學開始要進階時所要了解的幾個重點。

TRANSCRIPT

Page 1: 學好 node.js 不可不知的事

學好 node.js    不可不知的事

Node.js -- 程式設計師的魔術棒

Ben  Lue

Page 2: 學好 node.js 不可不知的事

⼀一切因它⽽而起

List<Map<String,  T>>  list  =      new  ArrayList<Map<String,  T>>();

Page 3: 學好 node.js 不可不知的事

Javascript  Says:

var    list  =  [];

Page 4: 學好 node.js 不可不知的事

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

Page 5: 學好 node.js 不可不知的事

So  This  Is  Node.js

It  is  like  a  963  HP  Ferrari  

Page 6: 學好 node.js 不可不知的事

Javascript

o Spec:  ECMAScript  EdiYon  5  o 我們熟知的 Javascript  事實上是 ECMAScript  +  Host  Environment.  

o Host  Environment:  在  browser  上就是  windows  物件

o Host  Environment:  在 node.js  上就是 Node.js  API,⼀一群內建的模組

Page 7: 學好 node.js 不可不知的事

Node  Modules

o 程式庫?套件?類別(class)? o 基本上⼀一個 .js  檔就能構成⼀一個 module  o 借助 npm  的幫忙,⼀一群  .js  檔可以擬似⼀一個module,並合作完成⾮非常複雜的功能。  

o ⼀一個模組就能夠成⼀一個程式  o 較複雜的程式需要許多模組來共同完成  

Page 8: 學好 node.js 不可不知的事

Module  解析

[The  require  secYon]  

[The  local  secYon]    Module  variables  &  Module  funcYons  

[The  export  secYon]    module.exports            |  exports.func_name  

Page 9: 學好 node.js 不可不知的事

Module  範例⼀一 var    h`p  =  require('h`p');    var    opYons  =  {  

 host:  'www.google.com',    port:  80,    path:  '/index.html'  

};    h`p.get(opYons,  funcYon(res)  {  

 res.setEncoding('ug8');    res.on('data',  funcYon(chunk)  {      console.log(  chunk  );    });  

}).on('error',  funcYon(e)  {    console.log('Error  as:  %s'  +  e.message);  

});  

Page 10: 學好 node.js 不可不知的事

Module  範例⼆二 var    h`p  =  require('h`p');    exports.load  =  funcYon(opYons,  callback)  {  

 h`p.get(opYons,  funcYon(res)  {      res.setEncoding('ug8');      res.on('data',  funcYon(chunk)  {        callback(  null,  chunk  );      });    }).on('error',  funcYon(e)  {      callback(  e  );    });  

};    

loadAsMod.js  

Page 11: 學好 node.js 不可不知的事

Module  範例三 var    loader  =  require(’./loadAsMod');    var    opYons  =  {  

 host:  'www.google.com',    port:  80,    path:  '/index.html'  

};    loader.load(opYons,  funcYon(err,  data)  {  

 if  (err)      console.log('Error  as:  %s'  +  e.message);    else      console.log(  data  );  

});  

useLoad.js  

Page 12: 學好 node.js 不可不知的事

exports  vs  module.exports

o ⼆二者不要同時使⽤用 o exports.xxx  公告模組中可使⽤用的函式  o module.exports  公告物件  o module.exports  如何使⽤用?⾒見下⾴頁範例  

Page 13: 學好 node.js 不可不知的事

攀越 Javascript  三座⼭山峰(3M)

o  this  o  Implement  class  (如何實作類別)  o callback  

Page 14: 學好 node.js 不可不知的事

Object

下⾯面三種物件的產⽣生⽅方式有什麼不同?  o var    a  =  {}  o var    b  =  Object.create()  o var    c  =  new  Claz()  

Page 15: 學好 node.js 不可不知的事

Prototype  and  Constructor

o Constructor:  物件起始化  o Prototype:  當物件找不到屬性時,會沿著  __proto__    chain  ⼀一路找回  

b  

__proto__  

a  

__proto__  

Page 16: 學好 node.js 不可不知的事

Object.create()

Object.create(protoObj,  propObj)  o 產⽣生⼀一個物件  o 指定  prototype  o empty  constructor  

Page 17: 學好 node.js 不可不知的事

var    a  =  {}

相當於 Object.create(Object.prototype)  o 產⽣生⼀一個物件  o 指定Object.prototype  為物件的 prototype  o empty  constructor  

Page 18: 學好 node.js 不可不知的事

var    c  =  new  Claz()

產⽣生物件時,⽤用 Claz.prototype  做為物件的  prototype,並呼叫 Claz()  來起始物件:    o var    c  =  Object.create(  Claz.prototype  );  o Claz.call(  c  );  

Page 19: 學好 node.js 不可不知的事

以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()  );  

Page 20: 學好 node.js 不可不知的事

夠⽤用,但缺了什麼…

o 能不能有  staYc  的變數? o 把函數都直接定義在物件  (this)  上,會吃

掉較多的記憶體和  CPU  cycles  

Page 21: 學好 node.js 不可不知的事

仿物件導向的程式樣板 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;  

Page 22: 學好 node.js 不可不知的事

其實這也是 node 模組的寫法

(funcYon  (exports,  require,  module,  __filename,  __dirname)  {      });  

Page 23: 學好 node.js 不可不知的事

繼承⽗父類別 funcYon  inherit(cls,  superCls)  {          //  不要直接指定 cls.prototype  =  new  superCls          var  construct  =  funcYon  ()  {};          construct.prototype  =  superCls.prototype;          cls.prototype  =  new  construct;          cls.prototype.constructor  =  cls;          cls.super  =  superCls;  }  

程式碼取材於h`p://stackoverflow.com/quesYons/1114024/constructors-­‐in-­‐javascript-­‐objects      

Page 24: 學好 node.js 不可不知的事

不過…

o 在 Javascript  上去模擬物件導向的繼承⽅方式是否真的有必要?  

o Prototype  chain  本⾝身已有繼承的功能  

Page 25: 學好 node.js 不可不知的事

Object  Property o Object.defineProperty(obj,  propName,  descriptors)  

o Descriptors:  {      value:  ”foo",      writable:  true,      enumerable:  true,      configurable:  true  }  

Page 26: 學好 node.js 不可不知的事

執⾏行和測試

o node  o Nodeclipse  o node-­‐inspector  

Page 27: 學好 node.js 不可不知的事

以 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  

Page 28: 學好 node.js 不可不知的事